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. Dismiss Notice

Question How to create a condition for my coroutine

Discussion in 'Scripting' started by minowhaat, May 22, 2023.

  1. minowhaat

    minowhaat

    Joined:
    Oct 27, 2022
    Posts:
    4
    How do i create a condition so that it only spawns 3 enemy at a time and will only only spawn new enemy if there is less than 3 enemy. below is my code, please help

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class EnemySpawner : MonoBehaviour
    {
    [SerializeField] private GameObject logPrefab;

    [SerializeField] private float logInterval = 10f;

    // Start is called before the first frame update
    void Start()
    {
    StartCoroutine(spawnEnemy(logInterval, logPrefab));
    }

    // Update is called once per frame
    void Update()
    {

    }

    private IEnumerator spawnEnemy(float interval, GameObject enemy)
    {
    yield return new WaitForSeconds(interval);
    GameObject newEnemy = Instantiate(enemy, new Vector3(Random.Range(2f, 6f), Random.Range(7f, 11f), 0), Quaternion.identity);
    StartCoroutine(spawnEnemy(interval, enemy));
    }
    }
     
  2. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    Hi, it's your first post, but please look around and try using code tags, because it makes the code formatted and much easier to read.

    Code (csharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3.  
    4. public class EnemySpawner : MonoBehaviour {
    5.  
    6.   [SerializeField] GameObject _prefab;
    7.   [SerializeField] float _timeInterval = 10f;
    8.   [SerializeField] [Min(1)] int _enemiesToSpawn = 3;
    9.  
    10.   int _enemyCount;
    11.   List<GameObject> _enemies;
    12.  
    13.   void Start() {
    14.     _enemyCount = 0;
    15.     _enemies = new List<GameObject>();
    16.     StartCoroutine(spawnEnemies);
    17.   }
    18.  
    19.   void Update() {
    20.     if(_enemyCount == _enemiesToSpawn) {
    21.       StopCoroutine(spawnEnemies);
    22.       printList(_enemies);
    23.       enabled = false;
    24.     }
    25.   }
    26.  
    27.   IEnumerator spawnEnemies() {
    28.     while(_enemyCount < _enemiesToSpawn)
    29.       yield return new WaitForSeconds(_timeInterval);
    30.       if(trySpawnNewEnemy(_prefab)) _enemyCount++;
    31.     }
    32.   }
    33.  
    34.   bool trySpawnNewEnemy(GameObject prefab) {
    35.     var newEnemy = Instantiate(prefab, new Vector3(Random.Range(2f, 6f), Random.Range(7f, 11f), 0), Quaternion.identity);
    36.     _enemies.Add(newEnemy);
    37.     return true; // true for success, could fail for some reason not covered here
    38.   }
    39.  
    40.   void printList<T>(IList<T> list) where T : UnityEngine.Object {
    41.     for(int i = 0; i < list.Count; i++) {
    42.       Debug.Log(list[i].name);
    43.     }
    44.   }
    45.  
    46. }
    Something like that.
    Some of this code is just decorational / illustrational (namely Update and printList).

    edit:
    added StopCoroutine
     
    Last edited: May 23, 2023
    seejayjames likes this.
  3. MatanYamin

    MatanYamin

    Joined:
    Feb 2, 2022
    Posts:
    109
    May I suggest not using coroutine for this case? It's a bit overkill for the situation.
    When an enemy is dead, I bet you have a function that does something. So in that function add an IF statement that checks the number of enemies. If you need to spawn a new one, just spawn a new one instantly.
    That way, you don't need to check inside the Update if the number has changed.
     
    Yoreki and orionsyndrome like this.
  4. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    Yes you may. I just did what OP asked for.
     
    Yoreki likes this.