It's a simple script really, I'm a beginner of Javascript and coded it myself.
#pragma strict
var HP : float = 100;
var body = gameObject;
function Update () {
if(HP < 1) {
var instance : GameObject = Instantiate(body, transform.position, transform.rotation);
DestroyObject (gameObject);
}
}
the script destroys the gameobject when the HP value is below 0, and spawns the body game object. but it cause problems and crashes the game when it's run through an exe.
it has these errors come up,
ArgumentException: get_gameObject can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
playerhealth..ctor () (at Assets/scripts/playerhealth.js:4)
get_gameObject can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
UnityException: You are not allowed to call this function when declaring a variable.
Move it to the line after without a variable declaration.
If you are using C# don't use this function in the constructor or field initializers, Instead move initialization to the Awake or Start function.
playerhealth..ctor () (at Assets/scripts/playerhealth.js:4)
I asked a similar question before, but it was vague and i didn't provide the script, it was also suggested by the errors to put the body variable into the update, but that causes way more problems than it fixes. I don't know what to do or how to fix it, I really need help with this.
↧