Unity freeze when applying this code

I would like something to spawn every 5 second, and i want to be able to change the delay.

Here is the code. The problem happens in Void SpawnTNT

using UnityEngine;
using System.Collections;

public class spawn : MonoBehaviour {

    public Transform[] spawnPoint;
    public GameObject tnt;
    public bool Spawning = true;
    // Use this for initialization
    void Start () {

        SpawnTNT ();
    }
   
    // Update is called once per frame
    void Update () {



   
    }

    void SpawnTNT ()
    {
        while (Spawning) {
            StartCoroutine (Example (1));
            Instantiate (tnt, spawnPoint [0].position, Quaternion.identity);
        }


    }

    IEnumerator Example(float delay) {
        print(Time.time);
        yield return new WaitForSeconds(delay);
        print(Time.time);
    }
}

Yup. Infinite loop. There is nothing to set Spawning false and exit your while loop.

I know. that i havn’t set spawning to false at any point

But how do i make it so it spawns every 5 second without freezing?

Put the loop INSIDE the coroutine.

You can do this more simply with InvokeRepeating:

using UnityEngine;

public class spawn : MonoBehaviour {

    public Transform[] spawnPoint;
    public GameObject tnt;
    
    void Start () {
        InvokeRepeating("SpawnTNT", 0, 5f);
    }

    void SpawnTNT ()
    {
        Instantiate (tnt, spawnPoint [0].position, Quaternion.identity);
    }
}
1 Like

Okay, i come so far:

using UnityEngine;
using System.Collections;

public class spawn : MonoBehaviour {

    public Transform[] spawnPoint;
    public GameObject tnt;
    public bool Spawning = true;
    // Use this for initialization
    void Start () {



    }
   
    // Update is called once per frame
    void Update () {
        StartCoroutine (SpawnTNT ());
    }

    IEnumerator SpawnTNT ()
    {
        if (Spawning) {
            Spawning = false;
            StartCoroutine (Example (1.0F));
            Instantiate (tnt, spawnPoint [0].position, Quaternion.identity);
            Spawning = true;
            yield return null;
        }

    }

    IEnumerator Example(float delay) {
        print(Time.time);
        yield return new WaitForSeconds(delay);
        print(Time.time);
    }
}

But now it wouldn’t change the bool Spawning back to false.
I just keep saying “True”
Why?

Anyone ? :wink:

StartCoroutine is a regular function call; it starts the coroutine but it returns immediately, i.e. it doesn’t wait for the coroutine to finish. So you’re setting spawning to false, then back to true again, within the same frame.

So superpig, how would you fix it?

Exactly the way I demonstrated 5 posts ago…?

1 Like

Oh Superpig didn’t saw that post! SORRY!

using UnityEngine;
using System.Collections;
public class spawn : MonoBehaviour {
    public Transform[] spawnPoint;
    public GameObject tnt;
    public bool Spawning = true;

    void Start () {
        StartCoroutine (SpawnTNT());
    }

    IEnumerator SpawnTNT () {
        while (true) {
            print(Time.time);
            yield return new WaitForSeconds(1f);
            print(Time.time);
            if (Spawning)
                Instantiate(tnt, spawnPoint[0].position, Quaternion.identity);
        }
    }
}