Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Adding delay in script. *notworking*

Discussion in 'Scripting' started by Ncon123, Aug 18, 2015.

  1. Ncon123

    Ncon123

    Joined:
    May 6, 2015
    Posts:
    25
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GameController : MonoBehaviour
    5. {
    6.     public GameObject Enemy;
    7.     public GameObject Player;
    8.     public float sd = 7f;
    9.     public float dia = 0.005f;
    10.     public float[] yValues = new float[] {-3.5f, 0f, 3.5f};
    11.     private float random;
    12.     int i = 1;
    13.  
    14.     void Update()
    15.     {
    16.         random = yValues [(int)Random.Range (0, yValues.Length)];
    17.         sd -= dia * Time.deltaTime; // spawn them quicker over time
    18.     }
    19.  
    20.     void enemySpawn()
    21.     {
    22.         Instantiate (Enemy, new Vector2 (random, 15), transform.rotation); // spawn enemy
    23.     }
    24.  
    25.     IEnumerator delay()
    26.     {
    27.         while (true)
    28.         {
    29.             yield return new WaitForSeconds(5); // wait function
    30.         }
    31.     }
    32.  
    33.     void wave1()
    34.     {
    35.         print ("wave 1 starting.");
    36.  
    37.         for(int i = 1; i < 10; i++)
    38.         {
    39.  
    40.         StartCoroutine(delay ());
    41.  
    42.         enemySpawn ();
    43.         enemySpawn ();
    44.  
    45.         }
    46.     }
    47.  
    48.     void Start()
    49.     {
    50.         Instantiate (Player, new Vector2 (0, 0), Quaternion.identity); // spawn player
    51.  
    52.         StartCoroutine(delay ());
    53.  
    54.         wave1 ();
    55.     }
    56.  
    57. }
    58.  
    Sorry for creating alot of this!
    Basicly the delay function does F*** all :L
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    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:

    Code (csharp):
    1.     IEnumerator wave1()
    2.     {
    3.         print ("wave 1 starting.");
    4.      
    5.         for(int i = 1; i < 10; i++)
    6.         {
    7.             yield return new WaitForSeconds(sd);
    8.  
    9.             enemySpawn ();
    10.             enemySpawn ();
    11.         }
    12.     }
    13.  
    14.     IEnumerator Start()
    15.     {
    16.         Instantiate (Player, new Vector2 (0, 0), Quaternion.identity); // spawn player
    17.  
    18.         yield return new WaitForSeconds (5.0f);
    19.  
    20.         yield return StartCoroutine (wave1 ());
    21.     }
    22.  
    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.
     
  3. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    The delay coroutine that you have setup doesn't need the while (true) statement, this will cause it to run indefinitely and never exit.
     
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,850
    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
      Code (csharp):
      1. yield return StartCoroutine
    The following will spawn 10 waves with 5 second delays:


    Code (csharp):
    1. IEnumerator Start()
    2. {
    3.     Instantiate (Player, new Vector2 (0, 0), Quaternion.identity); // spawn player
    4.    
    5.     for(int i = 0; i < 10; ++i)
    6.     {  
    7.         yield return new WaitForSeconds(5); // wait function
    8.         enemySpawn();
    9.     }
    10. }
     
  5. Ncon123

    Ncon123

    Joined:
    May 6, 2015
    Posts:
    25

    Thanks so much man, real big help
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    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. :)