Search Unity

Boolean not changing value in update

Discussion in 'Documentation' started by Gamrekeli, Jan 14, 2019.

  1. Gamrekeli

    Gamrekeli

    Joined:
    Aug 29, 2018
    Posts:
    2
    Hello, I'm creating an endless runner game where the player jumps to evade enemies. A score is displayed in the background. The score starts from one. Each jump increases the counter by one. What I want to happen is that when the counter is below 30 only the first wave of enemies keep spawning and when the counter hits 30 the first wave of enemies is over and the second wave of enemies starts. However, the waves don't seem to start when I press play and I don't have an idea on how to fix this. Any help would be appreciated!
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GameObjectSpawner : MonoBehaviour {
    5.  
    6.     public GameObject gameObj0;
    7.     public GameObject gameObj1;
    8.     public bool firstPhase;
    9.     public bool secondPhase;
    10.     private float score = ScoreScript.scoreValue;
    11.  
    12.     void Start()
    13.     {
    14.         firstPhase = false;
    15.         secondPhase = false;
    16.         StartCoroutine("SpawnGameObjects");
    17.     }
    18.  
    19.     void Update()
    20.     {
    21.         if (score < 30)
    22.         {
    23.             firstPhase = true;
    24.         }
    25.         if (score >= 30)
    26.         {
    27.             firstPhase = false;
    28.             secondPhase = true;
    29.         }
    30.        
    31.     }
    32.  
    33.     public IEnumerator SpawnGameObjects()
    34.     {
    35.         while (firstPhase == true)
    36.         {
    37.             Instantiate(gameObj0, new Vector2(GetComponent<Transform>().position.x, GetComponent<Transform>().position.y + Random.Range(0f, 40f)), Quaternion.identity);
    38.             yield return new WaitForSeconds(2);
    39.         }
    40.         while (secondPhase == true)
    41.         {
    42.             Instantiate(gameObj1, new Vector2(GetComponent<Transform>().position.x, GetComponent<Transform>().position.y + Random.Range(0f, 40f)), Quaternion.identity);
    43.             yield return new WaitForSeconds(2);
    44.         }
    45.     }
    46. }
    47.  
     
  2. Knarhoi

    Knarhoi

    Joined:
    Apr 11, 2015
    Posts:
    10
    You initialize score in start, but you don't seem to be updating the score variable.
    Try adding score = ScoreScript.scoreValue; in the update method.
     
  3. Gamrekeli

    Gamrekeli

    Joined:
    Aug 29, 2018
    Posts:
    2
    Nope, didn't work for me. In the inspector I can see that firstPhase is enabled when I press play, so shouldn't the first wave of enemies start spawning?

    I forgot to mention that the score is managed from another script. Here's the code just in case.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class ScoreScript : MonoBehaviour {
    7.  
    8.     public static int scoreValue;
    9.     Text score;
    10.    
    11.     void Start () {
    12.         scoreValue = -1;
    13.         score = GetComponent<Text>();
    14.     }
    15.    
    16.     void Update () {
    17.         score.text = "" + scoreValue;
    18.     }
    19. }