Adding delay in script. *notworking*

using UnityEngine;
using System.Collections;

public class GameController : MonoBehaviour
{
    public GameObject Enemy;
    public GameObject Player;
    public float sd = 7f;
    public float dia = 0.005f;
    public float[] yValues = new float[] {-3.5f, 0f, 3.5f};
    private float random;
    int i = 1;

    void Update()
    {
        random = yValues [(int)Random.Range (0, yValues.Length)];
        sd -= dia * Time.deltaTime; // spawn them quicker over time
    }

    void enemySpawn()
    {
        Instantiate (Enemy, new Vector2 (random, 15), transform.rotation); // spawn enemy
    }

    IEnumerator delay()
    {
        while (true)
        {
            yield return new WaitForSeconds(5); // wait function
        }
    }

    void wave1()
    {
        print ("wave 1 starting.");

        for(int i = 1; i < 10; i++)
        {

        StartCoroutine(delay ());

        enemySpawn ();
        enemySpawn ();

        }
    }

    void Start()
    {
        Instantiate (Player, new Vector2 (0, 0), Quaternion.identity); // spawn player

        StartCoroutine(delay ());

        wave1 ();
    }

}

Sorry for creating alot of this!
Basicly the delay function does fuck all :L

Take a quick review of how coroutines are implemented in Unity3D. They are actually “subprocesses” almost, so they are used in a slightly different way than you are assuming above. It’s kinda subtle.

In any case, there’s plenty of reference to review on that. I took a moment and rewrote your last two function so that it ought to work properly now:

    IEnumerator wave1()
    {
        print ("wave 1 starting.");
     
        for(int i = 1; i < 10; i++)
        {
            yield return new WaitForSeconds(sd);

            enemySpawn ();
            enemySpawn ();
        }
    }
 
    IEnumerator Start()
    {
        Instantiate (Player, new Vector2 (0, 0), Quaternion.identity); // spawn player

        yield return new WaitForSeconds (5.0f);

        yield return StartCoroutine (wave1 ());
    }

Note that I deleted and do not use the delay() function.

I would recommend moving the adjustment of the “sd” variable so that it is more explicitly tied to the actual wave controlling loop in wave1(). Otherwise it will be very difficult to keep it “in sync” with how you want the pacing of the game to advance.

The delay coroutine that you have setup doesn’t need the while (true) statement, this will cause it to run indefinitely and never exit.

A few problems:

  • Your delay function is stuck in a infinite loop.
  • StartCoroutine will not pause the current code. If you want that you need to call yield return StartCoroutine

The following will spawn 10 waves with 5 second delays:

IEnumerator Start()
{
    Instantiate (Player, new Vector2 (0, 0), Quaternion.identity); // spawn player
   
    for(int i = 0; i < 10; ++i)
    {  
        yield return new WaitForSeconds(5); // wait function
        enemySpawn();
    }
}

Thanks so much man, real big help

You’re welcome! Unity has a very slick “lightweight” system for this sort of pseudo-multitasking that really works nicely, and once you get comfortable with it, you’ll find lots of ways to abuse it, er, I mean use it. :slight_smile: