Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by eeveelution8 · May 04, 2014 at 04:54 AM · javascriptscript.variablescript errorfunction update

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image eeveelution8 · May 04, 2014 at 05:22 AM 0
Share

Ohhh, so it should have been GameObject, not gameObject, I see.

avatar image eeveelution8 · May 04, 2014 at 05:22 AM 0
Share

Thanks again for the help

avatar image AlucardJay · May 04, 2014 at 05:26 AM 0
Share

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;
avatar image eeveelution8 · May 04, 2014 at 08:48 PM 0
Share

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

avatar image AlucardJay · May 05, 2014 at 12:50 AM 0
Share

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);
 }
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image eeveelution8 · May 04, 2014 at 05:07 AM 0
Share

it only instantiates it once, line 12 destroys the object with this script after it spawns the object

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to access variable from another function? 2 Answers

Operator '*' cannot be used with a left hand side of type 'int' and a right hand side of type 'Object'. 1 Answer

Changing Variable OnMouseClick? Help! 1 Answer

Change variable value of a script from another script. both in different Game object 1 Answer

Trivial question for Javascript/Unityscript experts: static variables? 4 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges