Bool not changing for some reason

If someone feels like helping me out, would be greatly appreciated :slight_smile:

I made for the Glitch Garden part of the course a custom spawner script.
In the script i have a method to sets a bool to false, just like the spawners Rick made.

In Start, it does work and sets the bool to true, the method is being called,
but for some reason it does not change the bool…
Its only called in Start, in Update (if false return) and in the while loop, like ricks spawners
I used debug.log to see if the method is being called, and it is…
I do not get it

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class CustomSpawner : MonoBehaviour
{
    [SerializeField] float totalLevelTimeAfterFirstSpawn = 20f;
    [SerializeField] float timeBeforeFirstSpawn = 5f;
    [SerializeField] float minSpawnInterval = 8f;
    [SerializeField] float maxSpawnInterval = 8.1f;
    [SerializeField] float spawnIntervalIncreaseAmount = 1f;
    [SerializeField] float spawnIntervalIncreaseTime = 40f;
    float spawnIntervalIncreaseTimeR;
    [SerializeField] Attacker[] attackerArray;

    [SerializeField] bool spawn;

    [SerializeField] GameObject spawner;
    [SerializeField] GameObject spawner1;
    [SerializeField] GameObject spawner2;
    [SerializeField] GameObject spawner3;
    [SerializeField] GameObject spawner4;
    [SerializeField] GameObject spawner5;


    Queue<int> _pool = new Queue<int>();
    int _number = 0;
    int attackerIndex = -1;
    GameTimer gameTimer;

    public float TimeBeforeFirstSpawn()
    {
        return timeBeforeFirstSpawn;
    }
    void Start()
    {
        spawn = true;
        spawnIntervalIncreaseTimeR = spawnIntervalIncreaseTime;
        gameTimer = FindObjectOfType<GameTimer>();
        gameTimer.SetGameTime(totalLevelTimeAfterFirstSpawn);

        if (SceneManager.GetActiveScene().name != "Scene_03_Glitch")
        {
            StartGame();
        }
    }

    private void Update()
    {
        if(spawn == false) { return; }

        spawnIntervalIncreaseTimeR -= Time.deltaTime;
        if(spawnIntervalIncreaseTimeR <= 0)
        {
            minSpawnInterval -= spawnIntervalIncreaseAmount;
            maxSpawnInterval -= spawnIntervalIncreaseAmount;

            spawnIntervalIncreaseTimeR = spawnIntervalIncreaseTime;
        }
    }
    public void StartGame()
    {
        Invoke("InvokeCoroutineAfterDelay", timeBeforeFirstSpawn);
    }
    void InvokeCoroutineAfterDelay()
    {
        SpawnAttacker();
        gameTimer.GameStarted();
        StartCoroutine(SpawnCoroutine());
    }
    IEnumerator SpawnCoroutine()
    {
        while (spawn)
        {
            yield return new WaitForSeconds(Random.Range(minSpawnInterval, maxSpawnInterval));

            SpawnAttacker();
        }
    }
    void SpawnAttacker()
    {
        attackerIndex++;

        if(attackerIndex < attackerArray.Length)
        {
            PickSpawnerToInstantiateEnemy(attackerArray[attackerIndex]);
        }
    }

    //pick spawner at random and child supplied attacker at that spawner
    public void PickSpawnerToInstantiateEnemy(Attacker attacker)
    {
        Attacker newAttacker;

        if (_pool.Count == 0)// if pool is empty
        {
            // generate new pool:
            _pool = GiveMeNonReapeatingRandomSet(0, 6);

            // make sure first number in pool is not the same as our last one:
            if (_pool.Peek() == _number)
            {
                _pool.Enqueue(_pool.Dequeue());
            }

        }

        // pull next number from the pool:
        _number = _pool.Dequeue();

        switch (_number)
        {
            case 5:
                newAttacker = Instantiate(attacker, spawner.transform.position, Quaternion.Euler(0f, -90f, 0f)) as Attacker;
                newAttacker.transform.parent = spawner.transform;
                break;
            case 4:
                newAttacker = Instantiate(attacker, spawner1.transform.position, Quaternion.Euler(0f, -90f, 0f)) as Attacker;
                newAttacker.transform.parent = spawner1.transform;
                break;
            case 3:
                newAttacker = Instantiate(attacker, spawner2.transform.position, Quaternion.Euler(0f, -90f, 0f)) as Attacker;
                newAttacker.transform.parent = spawner2.transform;
                break;
            case 2:
                newAttacker = Instantiate(attacker, spawner3.transform.position, Quaternion.Euler(0f, -90f, 0f)) as Attacker;
                newAttacker.transform.parent = spawner3.transform;
                break;
            case 1:
                newAttacker = Instantiate(attacker, spawner4.transform.position, Quaternion.Euler(0f, -90f, 0f)) as Attacker;
                newAttacker.transform.parent = spawner4.transform;
                break;
            case 0:
                newAttacker = Instantiate(attacker, spawner5.transform.position, Quaternion.Euler(0f, -90f, 0f)) as Attacker;
                newAttacker.transform.parent = spawner5.transform;
                break;
            default:
                Debug.Log("PickRandomSpawner Default reached");
                break;
        }
    }

    Queue<int> GiveMeNonReapeatingRandomSet(int min, int max)
    {
        HashSet<int> set = new HashSet<int>();
        while (set.Count != Mathf.Abs(max - min))
        {
            set.Add(Random.Range(min, max));
        }

        return new Queue<int>(set);
    }
    public void StopSpawning()
    {
        Debug.Log("stop spawning called");
        spawn = false;
    }
}

In the code you posted I don’t see any calls to the StopSpawning method, I suppose it gets called in another script since it is set to public, so your issue might not be in the script you provided.

1 Like

Hi, no the method is indeed called from another script, thats why i put in the debug.log,
to see if it got called at all, and it prints it to the console, so the method is being called.

Thats why i thought it was so strange and asked here.
If the debug.log works, it must be somewhere in this script?
I mean, i can link the one its being called from, just thought it wasnt needed,
since i tested if the method was called.

1 Like

Based on what you said I don’t think this is a coding issue, there are only two places where you set the value of your spawn bool; at the Start method, at the method that stops the spawning, and you can’t change it from another script, my only guess is that you have a duplicated script somewhere in the hierarchy.

1 Like

Since this was the last “problem” i ran into before finishing up the project,
i solved it by literally making a script with just a bool,
called that from the method in the script i linked here,
and set the if statements to that scripts bool, and now everything works.
not the right way, but i have no clue what else it would be.
Also not a duplicate script, i checked for that.
at first i thought maybe one of the while loops was stuck or whatever,
but checked if it kept being called with a debug.log that was not the case either.
Thanks for thinking along anyways :slight_smile:

I’m glad Yee was able to help you. :slight_smile:

Are the problems fixed now?


See also:

with the before mentioned workaround it does what its supposed to do, but im no wiser as to why the bool wont change when the method is clearly called. So lets mark this as resolved then

How did you check that the bool does not change during runtime?

I have it as a serialized field, to see it in the inspector.
I checked if the method (StopSpawning() in the bottom of the script)
was being called with a debug.log, it was called.
I started the game with the bool on false (in Start i set it to true that works)
because i see the checkbox in the editor enable.
If i manually disable the bool, the spawning stops, so that also works.
The only line that doesnt work is “spawn = false;” in StopSpawning.
I even tried to make it a public bool for testing purposes, to change it directly from the script
that calls StopSpawning, it still did not change, and is stuck on true.
There is nothing in the script that sets it to true apart from start.
No other scripts can interact with the bool, only to set it false.
after Yee said maybe a duplicate script i checked everything, thats also not the case.
I checked each while loop with a debug.log to see if it wasnt stuck in a loop or something.
I just dont understand it.
Dont spend too much time on this, i just put the bool alone in another script and call it with a getcomponent now, and it works now, just hoped to understand what was going wrong.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms