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 Dasnya · Feb 21, 2013 at 06:28 PM · variablesstatic

My static variable changes, but other scripts don't notice.

So I have two scripts. One is on a character, and when it "dies," I set a static variable "Dead" to true. This script is fine and everything works like it is supposed to.

 function OnTriggerEnter (hit : Collider){
     if((hit.gameObject.tag == "Spikes")&&(GoingToDie == false)){
         GoingToDie = true;
         var WinstonExplosion2 = Instantiate(WinstonExplosionPrefab, transform.position, transform.rotation);
         WinstonExplosion2.transform.parent = gameObject.transform;
         yield WaitForSeconds(1);
     Dead = true;
     GoingToDie = false;    
         
     }

 function LateUpdate (){
     if(Dead){//                                                                               reset on death
         print("dead");
         transform.position = GameObject.Find("Character_Spawn").transform.position;
         //Dead = false;
         // I know I'm not setting Dead back to false, but it doesn't work when I do that..
     }


Comment
Add comment · Show 5
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 Dasnya · Feb 21, 2013 at 05:21 PM 0
Share

But I also have another script attached to a level generating actor. When "Dead" is true, it's supposed to reset its position. However, it doesn't seem to be noticing when "Dead" is true.

 function Update () {
 
 if($$anonymous$$ainControl.Dead == true){
     transform.position.x = 447.5;
     Elevation = 0;
     print("levelgen position reset");
     Start();
     }
 }
avatar image ytanay · Feb 21, 2013 at 06:56 PM 0
Share

Can you place the "Dead" variable on a different GameObject and send a message to narrow down where the problem is?

avatar image Dasnya · Mar 01, 2013 at 04:59 PM 0
Share

Sorry! I haven't worked on this in a week. So I moved the "Dead" variable onto a "Test" script, and of course the console complains that "Dead" is not a part of $$anonymous$$ainControl wherever I had the variable being used. HOWEVER, I get no errors from the LevelGeneration script. I suppose that's where my problem is.

On testing with the variable placed elsewhere but the code changed to work as intended, I am still having problems with LevelGeneration.

It's as if my if($$anonymous$$ainControl.Dead == true) statement just isn't running.

avatar image ytanay · Mar 01, 2013 at 05:48 PM 0
Share

I wrote an answer below, but also keep in $$anonymous$$d that there is no need to evaluate a Boolean like this: if($$anonymous$$ainControl.Dead == true) - you should use if($$anonymous$$ainControl.Dead).

avatar image Bonfire-Boy · Mar 02, 2015 at 12:01 PM 0
Share

@ytanay There's no need to correct people's style. if ($$anonymous$$ainControlDead == true) is perfectly fine and some of us prefer it in certain situations. There are enough tricky things for people to learn and remember without trying to force our own personal choices on them!

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by pazzesco · Mar 01, 2013 at 05:40 PM

Static variables can be a dicey thing. I generally try to shy away from using them as much as possible, but if you must, try doing this instead:

Make the Dead static variable itself private, and change how it's accessed from as a variable to methods, like so:

 //Changed to private from public. Also changed name of variable itself to catch any places within this script it's being accessed without using GetDead()/SetDead().
 private static var IsDead:boolean = false;
 
 static function GetDead():boolean {
     return IsDead;
 }
 
 static function SetDead(newDead:boolean) {
     if (newDead == true) {
         Debug.Log("Dead being set to true...");
     } else {
         Debug.Log("Dead being set to false...");
     }
     IsDead = newDead;
 }

Doing it this way could provide a little more insight on where you could be accessing this, and may illuminate any logical errors within your code. You'll have to change code to reflect this new way, so:

 if(MainControl.Dead == true)
 
 // ...becomes...
 
 if(MainControl.GetDead() == true)

Hope this helps.

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 Bunny83 · Mar 01, 2013 at 07:12 PM 1
Share

Yes, that's why encapsulation is a good thing ;)

avatar image
2

Answer by ytanay · Mar 01, 2013 at 05:45 PM

I think your first problem is caused by the way Update methods are chained. There is no guarantee that LateUpdate on one script will run after Update on a different script.

In any case, this seems to be a weird way to accomplish what you are trying to do. What you should probably do is use GameObject.SendMessage (over here). Move the entire code under the logic in Update to a ResetPosition() method and call it with SendMessage. This will make sure that it will always get called, once. Never try to evaluate static variables that can change every frame - it's a recipe for disaster. In fact, I would suggest entirely avoiding static variables and methods in this case.

This will also solve a potential future problem, which is that it will get stuck on x = 447.5.

Let me know if this works (and especially if it doesn't)

Comment
Add comment · Show 2 · 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 Bunny83 · Mar 01, 2013 at 07:10 PM 0
Share

Actually the only purpose of LateUpdate is that you can be sure that all Update functions are called before any LateUpdate is called. LateUpdate

However the rest in your answer is actually right ;)

avatar image ytanay · Mar 01, 2013 at 08:33 PM 0
Share

Hmmm, that's interesting - I recall reading something else regarding the order, but of course this makes a lot more sense. Thanks for the info!

avatar image
0

Answer by Dasnya · Mar 01, 2013 at 06:56 PM

Alright, I figured it out.... There was nothing wrong with the static variables. I was setting "Dead" back to false in another script before all the other scripts ran, and I hadn't noticed.. But thank you ytanay and pazzesco for the suggestions! I'll be sure to do something about all those static variables!

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 idiot333 · Mar 02, 2015 at 11:41 AM 0
Share

hey @Dasnya , it doesn't work in my case. my static variable never changes from another scrip. i get the same value even if it executes successsfully

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

13 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

Related Questions

Can one use of a static variable, change it globaly? 2 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How can I edit my static variable in the editor? 1 Answer

passing variables through scripts 0 Answers

Static variables optimization 2 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