- Home /
Script is causing crashes and i Don't know what to do.
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.
Answer by AlucardJay · May 04, 2014 at 05:15 AM
Line 4, you're not typecasting, but instead giving the variable another undeclared variable. This should be :
var body : GameObject; // variable name : Type;
Line 12, is this a custom function that is called to destroy? if not you need :
Destroy( gameObject ); // destroys this gameObject
http://docs.unity3d.com/Documentation/ScriptReference/Object.Destroy.html
Ohhh, so it should have been GameObject, not gameObject, I see.
Yes, 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;
what 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
As 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);
}
Answer by SteelArrow21 · May 04, 2014 at 05:02 AM
Update function is called every frame. Line 10 Initiates a game object. If that happens every frame, then its gonna crash. Don't initiate the gameobject every frame.
it only instantiates it once, line 12 destroys the object with this script after it spawns the object
Your answer
![](https://koobas.hobune.stream/wayback/20220613144214im_/https://answers.unity.com/themes/thub/images/avi.jpg)