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

Coroutine Not Working

Discussion in 'Scripting' started by wwyliee, Mar 30, 2020.

  1. wwyliee

    wwyliee

    Joined:
    Jan 31, 2020
    Posts:
    7
    I'm Trying to get my enemies to spawn at random times and I can't seem to get the coroutine to work. Any help would be much appreciated.
    Thanks
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SpawnEnemy : MonoBehaviour
    6. {
    7.     public int speed;
    8.     public int minTime = 1;
    9.     public int maxTime = 8;
    10.     public GameObject Enemy;
    11.     public Transform SpawnPoint;
    12.     void FixedUpdate()
    13.     {
    14.        
    15.         StartCoroutine("Spawn");
    16.         Instantiate(Enemy, SpawnPoint.position, SpawnPoint.rotation);
    17.     }
    18.     IEnumerator Spawn()
    19.     {
    20.        yield return new WaitForSeconds(Random.Range(minTime, maxTime));
    21.        
    22.     }
    23.  
    24. }
    25.  
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Your coroutine doesn't do anything though. You're starting your coroutine, which will immediately run code at the start of the Spawn method, which all it does is yield return. As soon as that happens, which is immediately, it then goes back and runs the rest of FixedUpdate.

    Don't use FixedUpdate for anything other than the physics system by the way.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    The coroutine is working. It just does nothing but waits.

    You are also starting it again and again in FixedUpdate(), which runs perhaps 30-50 times per second, so I don't think you want that.

    Instead you probably want to call StartCoroutine() from within a void Start() function so it only fires once.

    Additionally, the coroutine should do two things: 1. wait for the amount of time, 2. spawn the enemy, which you are presently doing outside of the coroutine.

    Here's a cheatsheet for when "special functions" are called in Unity:

    https://docs.unity3d.com/Manual/ExecutionOrder.html
     
    Joe-Censored likes this.
  4. wwyliee

    wwyliee

    Joined:
    Jan 31, 2020
    Posts:
    7
    Thank you for the tips, they were really helpful, do you know how I could get it to spawn them at a slower rate, instead of just once or 50 times? Thanks again.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    You can actually put a loop in a coroutine if you want to just endlessly mindlessly spawn them:

    Code (csharp):
    1. while( true)
    2. {
    3.   yield return new WaitForSeconds( 5.0f);  // or whatever value you like
    4.   ... call the spawn function here
    5. }
    If you want more "intelligence" than that, such as "spawn one every 5 seconds unless there are already too many," then you would have a separate check (inside the above loop) that would say "Hey, how many enemies are there now? Not enough? Good! Spawn one!" Or else return to waiting for 5 seconds.
     
  6. wwyliee

    wwyliee

    Joined:
    Jan 31, 2020
    Posts:
    7
    Okay, that's exactly what I needed thank you, you are a life saver.
     
    Kurt-Dekker likes this.