• 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 /
  • Help Room /
avatar image
0
Question by Willops · Nov 07, 2016 at 07:41 AM · errorbooleanvalue

Bool not retaining value after being set in an invoke function.

I'm having a strange error in which a boolean is not retaining it's value after I set it.

The bool I'm having the issue with here is timePaused, everything else is mostly irrelevant.

 public float timeLeft = 10.0f;
 public bool timePaused;

void Awake () {

     timePaused = true;
     
     if (instance == null)
     {
         instance = this;
     }
     else if (instance != this)
     {
         Destroy(gameObject);
     }

     DontDestroyOnLoad(gameObject);
     enemies = new List<Enemy>();

     
     mainMenu = GetComponent<MainMenu>();
     mainMenu.SetupMenu();
 }

public void InitGame() {

     doingSetup = true;
     timePaused = true;

     levelImage = GameObject.Find("LevelImage");
     
     levelText = GameObject.Find("LevelText").GetComponent<Text>();



     levelText.text = "Floor " + level;

     
     Invoke("HideLevelImage", levelStartDelay);


     enemies.Clear();
     boardScript = GetComponent<BoardManager>();
     boardScript.SetupScene(level);  
 }

private void HideLevelImage() {

     levelImage.SetActive(false);
     doingSetup = false;
     timePaused = false;
     if (timePaused == false)
     {
         Debug.Log("Time unpaused");
     }
 }

void Update () {

     if (timePaused == true)
     {
         Debug.Log("Time paused");
     }

     //Timer
     if (timePaused == false)
     {
         timeLeft -= Time.deltaTime;

         timerText = GameObject.Find("Timer").GetComponent<Text>();

         timerText.text = timeLeft.ToString("F3");
     }

     if (timeLeft < 0)
     {
         GameOver();
     }

     if (timeLeft <= 3)
     {
         timerText.color = Color.red;
     }

     //Moving enemies
     if (playersTurn || enemiesMoving || doingSetup)
     {
         return;
     }
     StartCoroutine(MoveEnemies());

 }

private void OnLevelWasLoaded (int index)

 {
     level++;

     timeLeft = 10.0f;

     InitGame();
 }

I'm very new to Unity, so sorry if this code is atrocious.

What should happen is, the game starts and time is paused until the player (In mainMenu.SetupMenu) starts the game. Then the game displays the floor number (In this case floor 1) for about 2 seconds (levelStartDelay) and then deactivates the level image and unpauses the game.

However, in HideLevelImage timePaused returns as false, but in Update timePaused returns as true, and so time never gets unpaused.

And when the player finishes the first stage, and OnLevelWasLoaded is called, the timer functions just as it should, pausing to display the floor number, and then unpausing when the game starts.

Why is my bool acting so wonky?

(I think I've got all of the necessary info here, since no other scripts interact with timePaused, but I can give more if it's needed)

Comment
Add comment · Show 1
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 Willops · Nov 09, 2016 at 05:41 PM 0
Share

A few more details, if I don't go to Setup$$anonymous$$enu(); on awake and ins$$anonymous$$d go directly to InitGame(); the timer will work fine.

P.S. I'm no longer really working on this project, but if anyone wants to solve it, it may help people in the future.

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

90 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 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 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 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

List of Structs variable values not changing 1 Answer

I made this script instance but I get this error: Assets / Prefab / PrefabSpawner.cs (15,12): error CS0135: `PrefabPosL 'Conflicts with a declaration in a child block 1 Answer

How do I check if bool is true from another script? 2 Answers

How To Fix Parsing Errors 1 Answer

unity 5.2.1f1 error result->dynamic bounds 0 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