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.
Ohhh, so it should have been GameObject, not gameObject, I see.
– eeveelution8Thanks again for the help
– eeveelution8Yes, declaring variables is var Name : (semicolon) then Type;, and then maybe = someValue;. Types that are components are usually assigned in the inspector, or using GetComponent in the Start function. var rBody : RigidBody; var tx : Transform; var myFloat : float = 1.2; var myInt : int = 5;
– AlucardJaywhat about get.component variables, I have a line of a script that goes, var score : Deathmatchinfo = gameObject.GetComponent(Deathmatchinfo); and the same thing comes up
– eeveelution8As in your other question, variables should only be assigned in the Inspector or in a function : var score : Deathmatchinfo; function Start() { score = gameObject.GetComponent(Deathmatchinfo); }
– AlucardJay