Search Unity

(Fixed / Answered) - Regarding IEnumerator

Discussion in 'Scripting' started by Kazzack, Aug 14, 2014.

  1. Kazzack

    Kazzack

    Joined:
    Mar 8, 2013
    Posts:
    27
    Hey guys, I'm trying to use IEnumerator so I can get certain scripts / functions to wait before executing, and I really don't understand why this is being such a pain. It's so simple in UnityScript and then just seems stupidly overcomplex in C#...

    Would love some help or explanation on this...

    Code (CSharp):
    1. void Update ()
    2.     {
    3.         if(playerInZone == true && canSpawn == true)
    4.         {
    5.             StartCoroutine("SpawnZombie");
    6.         }
    7.     }
    8.  
    9.     IEnumerator SpawnZombie()
    10.     {
    11.         Debug.Log ("Waiting 15 seconds....");
    12.         yield return new WaitForSeconds (15);
    13.         int temp;
    14.         temp = Random.Range(0, spawnSpots.Count);
    15.         Instantiate(ZombiePrefab, spawnSpots[temp].transform.position, spawnSpots[temp].transform.rotation);
    16.         zombieCount ++;
    17.         Debug.Log ("Spawning a zombie now");
    18.     }
    These is my code, what i'm trying to make it do is, if the player is in a certain zone and they are allowed to spawn, then every 15 seconds (for example), choose a random spawn location (working fine) and spawn 1 of them.

    But at the minute it waits the seconds at the start and then spawns up to the max zombie's or waaaaay above without waiting for the seconds.. Any idea's?
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    because the StartCoroutine is in the Update function, it is starting the coroutine every frame.
    You need to stop calling StartCoroutine after the first time. eg:
    bool spawning = false;
    if(playerInZone == true && canSpawn == true && spawning == false)....
    spawning = true
     
    GarthSmith likes this.
  3. zee_ola05

    zee_ola05

    Joined:
    Feb 2, 2014
    Posts:
    166
    Because you're calling it in your Update method.

    What you can do is have a canSpawn flag, coroutine that tracks the cooldown, and a spawn function. Something like this...

    Code (CSharp):
    1. IEnumerator StartCooldownTimer()
    2. {
    3.     canSpawn = false;
    4.     yield return new WaitForSeconds(15f);
    5.     canSpawn = true;
    6. }
    7.  
    8. void AttemptSpawn()
    9. {
    10.     if(!canSpawn)
    11.         return;
    12.     StartCoroutine(StartCooldownTimer());
    13.  
    14.     // put spawn code here
    15. }
    16.  
    17. void Update()
    18. {
    19.     if(playerInZone == true && canSpawn == true)
    20.         AttemptSpawn();
    21. }
     
  4. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    5,041
  5. Kazzack

    Kazzack

    Joined:
    Mar 8, 2013
    Posts:
    27
    Okay thank you all for your replies.
    It makes sense now :)
    And thank you @JohnnyA, I never knew about Invoke Repeating, so I'll give it a shot :)

    // Closed (unless there is a way to actually close a thread? :) )