Search Unity

While Loop Not Running When Set True in Code, But Will Run If Set Manually In Unity

Discussion in 'Scripting' started by mooman04, Dec 22, 2017.

  1. mooman04

    mooman04

    Joined:
    Oct 7, 2017
    Posts:
    2
    Hello All,

    I have the following problem with the below script. I have a powerup in my game called Freeze that is freezing the spawning of enemies by setting a boolean spawnActive variable in another script to false which stops them spawning. This part is working exactly how i want it to, however in the below script the while loop should run when freezeActive is set to true which i am doing in OnMouseDown(), so when a player clicks the freeze power up the loop should run.

    This will not work when set in this way, if i write in at the top public bool freezeActive = true instead of false this will work. Also if I click the freezeActive variable on inside Unity manually it will work.

    I can even see the freezeActive variable being ticked on in unity when I click the power up, but the loop will not run.

    Any assistance would be greatly appreciated :)

    Have a great xmas!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class freeze : MonoBehaviour {
    6.  
    7.     public bool freezeActive = false;
    8.  
    9.     //starting timer for freezeTime
    10.     void Start()
    11.     {
    12.         StartCoroutine (freezeTime());
    13.         Debug.Log ("timer Started");
    14.     }
    15.  
    16.     //find the spawnActive and stop it, set clicked to true
    17.     void OnMouseDown()
    18.     {
    19.         FindObjectOfType<spawner>().spawnActive = false;
    20.         FindObjectOfType<powerUpSFX>().playPowerUpSFX();
    21.         freezeActive = true;
    22.     }
    23.        
    24.     //clicked is set to true so the loop runs, it should wait 5 seconds and then set spawn active back on
    25.     IEnumerator freezeTime()
    26.     {
    27.         while (freezeActive)
    28.         {
    29.             yield return new WaitForSeconds(5);
    30.             FindObjectOfType<spawner>().spawnActive = true;
    31.             freezeActive = false;
    32.         }
    33.     }
    34.  
    35.     // Update is called once per frame
    36.     void Update () {
    37.         transform.Rotate(new Vector3 (1, 1, 0));
    38.     }
    39. }
     
  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    when your script starts, it starts the coroutine, and because freezeTime is set to false, it immediately drops the coroutine.

    The simple fix is like so:

    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class freeze : MonoBehaviour {
    6.  
    7.     public bool freezeActive = false;
    8.  
    9.     //starting timer for freezeTime
    10.     void Start()
    11.     {
    12.         StartCoroutine (freezeTime());
    13.         Debug.Log ("timer Started");
    14.     }
    15.  
    16.     //find the spawnActive and stop it, set clicked to true
    17.     void OnMouseDown()
    18.     {
    19.         FindObjectOfType<powerUpSFX>().playPowerUpSFX();
    20.         freezeActive = true;
    21.     }
    22.        
    23.     //clicked is set to true so the loop runs, it should wait 5 seconds and then set spawn active back on
    24.     IEnumerator freezeTime()
    25.     {
    26.         while (true)
    27.         {
    28.             if(freezeTime){
    29.                 FindObjectOfType<spawner>().spawnActive = false;
    30.                 yield return new WaitForSeconds(5);
    31.                 FindObjectOfType<spawner>().spawnActive = true;
    32.                 freezeActive = false;
    33.             } else {
    34.                 yield return null;
    35.             }
    36.         }
    37.     }
    38.  
    39.     // Update is called once per frame
    40.     void Update () {
    41.         transform.Rotate(new Vector3 (1, 1, 0));
    42.     }
    43. }
     
    Ryiah and mooman04 like this.
  3. mooman04

    mooman04

    Joined:
    Oct 7, 2017
    Posts:
    2
    You sir, are a genius!

    Thank you kindly!

    Merry Christmas