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

Time Delay Not Working, not to complicated?

Discussion in 'Scripting' started by DanSingh, Jul 4, 2016.

  1. DanSingh

    DanSingh

    Joined:
    Feb 5, 2015
    Posts:
    98
    Greetings, I am working on a script where it would instantiate an object every 1.5 sec however, it seems to not wait and just does it one go. Please help.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ZombieSpawnCont : MonoBehaviour
    5. {
    6.     public GameObject zombiePrefab;
    7.  
    8.     // Use this for initialization
    9.     void Start ()
    10.     {
    11.  
    12.     }
    13.  
    14.     public void SpawnZombie(Transform loc, int numOfZom)
    15.     {
    16.         for(int i=1;i<=numOfZom;i++)
    17.         {
    18.             GameObject tempZomb = Instantiate(zombiePrefab,loc.transform.position,loc.transform.rotation) as GameObject;
    19.             StartCoroutine(Timer());
    20.         }
    21.     }
    22.  
    23.     IEnumerator Timer()
    24.     {
    25.         yield return new WaitForSeconds(1.5f);
    26.     }
    27. }
    28.  
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    That's not how it works.
    At the moment, you start 'numOfZom' coroutines in the same frame.

    The following starts the coroutine once. If you need that in Update, make sure you only start when necesssary and under given conditions, not every frame. E.g. track if it currently running etc.

    Code (CSharp):
    1. public class ZombieSpawner : MonoBehaviour
    2. {
    3.     public GameObject zombiePrefab;
    4.  
    5.     void Start()
    6.     {
    7.         StartCoroutine(SpawnZombies(transform, 5));
    8.     }
    9.  
    10.     IEnumerator SpawnZombies(Transform loc, int number)
    11.     {
    12.         for (int i = 0; i < number; i++)
    13.         {
    14.             yield return new WaitForSeconds(1.5f);
    15.             Instantiate(zombiePrefab, loc.transform.position, loc.transform.rotation);
    16.         }
    17.     }
    18. }
     
  3. DanSingh

    DanSingh

    Joined:
    Feb 5, 2015
    Posts:
    98
    Okay Thanks
     
  4. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    Remember, if you're using IEnumerator, you need to type what you want it to do after the time is up. In your case:

    Code (CSharp):
    1.  
    2. IEnumerator Timer()
    3.  
    4. {
    5.        yield return new WaitForSeconds(1.5f);
    6.     //do what you want after 1.5 seconds
    7. }
    8.