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 blaz0135 · Mar 09, 2014 at 11:32 AM · c#update functionglobal variable

Global Variable Resseting By Itself

So as the question title says I have a script with a "global" boolean variable and it gets set to false (the declaration value) without being set back to it.

A small example below: (by the way I am using C#) (also

 private bool gameStarted = false;
  
     void Update() {  
         if(gameStarted == false){
             buildGame();
         }
         if(gameStarted){
             //...
         }
     }
  
     void buildGame() {
         //...  
         gameStarted = true;
     }
 

I even put print() before and after each reference of gameStarted and it displays that at the beginning of update gameStarted is false with gameStarted being true at the end but on the very next update call it is false again with no instance in the code (even using shift + f12 to find all references) of anything setting the variable back to false. Does anyone know what I should do? Because other than restarting and going bit by bit through the code I don't see any way of knowing why this is doing it.

Edit: sorry if the formatting is bad it wont let me make it look better / I don't know how to let it let me make it look better.

Comment
Add comment · Show 4
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 whydoidoit · Mar 09, 2014 at 11:34 AM 0
Share

To format your code, paste it in as normal and the hit the 101010 button in the toolbar to format it as code. What this does is ensure that all code is indented by 4 spaces (or you can use 1 tab).

avatar image whydoidoit · Mar 09, 2014 at 11:35 AM 2
Share

Something else has to be changing it! Can you post the whole of this code? Any reason why you are not doing buildGame in Start?

avatar image Jamora · Mar 09, 2014 at 12:04 PM 0
Share

@blaz0135 you were using block quotes to format the code; >>>. You may have pressed the wrong button. The correct one is the 1010101 one. I formatted the code for you.

avatar image blaz0135 · Mar 09, 2014 at 01:18 PM 0
Share

@Jamora I tried to use the numbers and it didn't look right so I tried to use the blockquote (") and I guess made it worse. Thank you for fixing it though.

@whydoidoit The problem with posting all of the code is I am untrained with separating the code into multiple scripts so everything is in 1 script and there is alot. The reason I wasn't using it in Start was again with the one script I was building it to where the menu screen would be built and shown from start and then once from there the game has been selected to start the game would then build the level.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by jasperstocker · Mar 09, 2014 at 03:14 PM

You might want to Serialize that private field.
http://docs.unity3d.com/Documentation/ScriptReference/SerializeField.html
TLDR:
[SerializeField]
private bool gameStarted = false;

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 blaz0135 · Mar 09, 2014 at 04:49 PM 0
Share

Unfortunately no that didn't do anything but when used it did stop the boolean from being set to false at all, beginning and later.

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

23 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 avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Issues sharing variable between 2 scripts 2 Answers

How can I reset a rigidbody? 1 Answer

Function is not updating the variable 1 Answer


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