Can't find a way to add lag time before spawns

Hey There!

I’ve been enjoying the glitch garden part of the course so far. Completed most of the challenges without having to cheat. :smiley:

Here’s my problem though: I want to add some time before the attackerspawners start spawning attackers. Currently it has a minimum and maximum time for spawn. I can technically delay the first spawn by making the minimum time large. However, if I want to then have fast spawns after the initial spawning delay is done, it falls apart.

Here are some of the ways I have tried and failed till now:

1> Inactivate the spawners in the editor to start with. Then have a separate script findobjectsoftype and activate them after a set delay. Or maybe instantiatefield an array of spawners that can be set gameObject.SetActive(true).

2> I tried making the bool spawn ‘false’ to initiate and then use update to check against a time.timesincelevelload timer to set it to true after some time. This however does not work. Setting the bool from true to false can stop the coroutene but it will not start if its false at start and then is turned on midway. I guess this is because the start is called in the beginning and if it doesnt start the coroutene, it is never called again.

Is there a way to call the coroutene from anywhere except start?

Or maybe there is a much simpler way to do this and I cannot think of it.

Thank you for your help in advance :slight_smile:

I have tried to add the coroutene to the update method. But then it just keeps calling every frame and it gets reset I guess?

One way to get the additional delay and have control over it would be to add an additional waitforseconds in the Start function of AttackerSpawner.cs as shown below. A new property is also required to allow controlling the delay on a per spawner basis


    [SerializeField] float spawnStartingDelay = 0f; // use a default of 0 or no delay... 

    IEnumerator Start()
    {
        
        yield return new WaitForSeconds(spawnStartingDelay); //Added this line to the Start function in 
        

        while(spawn)
        {
            float waiting = Random.Range(minSpawnDelay, maxSpawnDelay);
        
            yield return new WaitForSeconds(waiting);
        
            SpawnAttacker();
        }
    }

Then in the editor you just set the required starting delay in the properties of the attack spawner you want to have a start delay.

image

Sample run - Here the fox spawner had 0 start delay and very short min/max spawn delay and the crocodile spawner had a 5 start second start delay along with a very short min/max spawn delay.

1 Like

You could call it from update, if you make a bool or something to make it only be called once.
So when the coroutine activates, it set “spawning” to true and then above it;

if(spawning) {return;} 

Or create a new method where you call your coroutine and invoke that from start like

 Invoke("MethodToCallCoroutineName", 5f);

Or i guess just to child your spawners under 1 gameobject,
which is disabled at start, with a scripts on it that sets that active after X time
could create an empty gameobject with this on it for example

    [SerializeField] float spawnDelay = 5f;
    [SerializeField] GameObject spawnersParent;
    void Start()
    {
        StartCoroutine(ActivateSpawners());
    }

IEnumerator ActivateSpawners()
    {
        yield return new WaitForSeconds(spawnDelay);
        spawnersParent.SetActive(true);
    }

But maybe others have a better idea, im still learning too xD

1 Like

Thank you so much. That solves the problem. I thought that I cannot call more than one yield return per coroutine. Cheers!

1 Like

Thank you. I did end up creating something like your third suggestion by myself. Cheers! :slight_smile:

1 Like

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

Privacy & Terms