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 Ivexian · Jan 12, 2013 at 12:41 AM · updatestaticif-statementsifglobal

Update Function Won't Do If Statements

We have a static var bCounter. bCounter is initialized to 0, and will sometimes be incremented in another function. Every time our script updates it goes through a serious of ifs to determine what to do based on bCounter. However, our script's update function won't even recognize when bCounter is 0 (which we are testing for using the print statements.) Why won't the update even do the first if statement?

 function Update () 
 {
 if (bCounter == 0)
     print("Still this far!");
 else if (bCounter == 1)
 {
     print("Good job!");
     if (bThirteen == true && Time.time - temp13 < 1)
         {
         Stair13.transform.Translate(0,Time.deltaTime * 13.73371,0);
         }
     else if(bThirteen == false)
         bCounter = 0;
 }
Comment
Add comment · Show 12
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 save · Jan 12, 2013 at 12:45 AM 0
Share

Are you absolute positive that bCounter is 0 at start?

avatar image Wolfram · Jan 12, 2013 at 12:46 AM 2
Share

Impossible to tell unless you tell us when, how, and under which conditions bCounter is incremented/modified.

For starters, put print("Time "+Time.time+", bCounter="+bCounter); at the very beginning of this Update().

Also remember that if this is an Editor script or has "ExecuteInEdit$$anonymous$$ode" set, your static var will never be reset.

avatar image Ivexian · Jan 12, 2013 at 12:47 AM 0
Share

100%. It is initialized to 0 in Start().

avatar image Ivexian · Jan 12, 2013 at 12:50 AM 0
Share

Here is when bCounter is incremented. This is in another script. We have checked within this script to see if bCounter's value is changed, and it does. (in the update function for this script we had an if statement that would print if it was equal to 1)

function On$$anonymous$$ouseDown() { transform.Translate(0,0,-.3); if (NumberButton.bThirteen == false) { NumberButton.bThirteen = true; NumberButton.temp13 = Time.time; NumberButton.bCounter += 1;
} }

avatar image You! · Jan 12, 2013 at 12:50 AM 0
Share

Is the increment function in a different script? If so, the other script may be only making a copy of the variable, not transferring the value to your Update function.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Wolfram · Jan 12, 2013 at 01:24 AM

To prevent the problem of forgetting to attach a script with static variables to a scene object (which can happen since the static variable "exists", even if the script is not in the scene), instead of using static variables create a "Manager" or "Controller" script, put it exactly once in your scene, and move all these globally accessible variables to that manager. Then make these variables non-static.

Comment
Add comment · 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

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

12 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

Related Questions

only updating number vars once 1 Answer

what does Static Function exactly do? 2 Answers

How to use same code at multiple scenes ? 1 Answer

If statements in update 1 Answer

How can I preserve static object/data between editor and play? 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