NEED HELP PLEASE: When i kill a one of the three limited "mobSpawned" the "while (true)" don't work and don't spawn a new object. Anyone have a solution for this. Thanks.
using UnityEngine;
using System.Collections;
public class SpawnManager : MonoBehaviour
{
public GameObject mobPrefabs;
public Vector3 spawnValues;
public int mobCount;
public int mobSpawned;
public float spawnWait;
public float startWait;
public float waveWait;
void Start ()
{
StartCoroutine (SpawnWaves ());
}
IEnumerator SpawnWaves ()
{
yield return new WaitForSeconds (startWait);
while (true)
{
for (int i = 0; i < mobCount; i++)
{
Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
Quaternion spawnRotation = Quaternion.identity;
Instantiate (mobPrefabs, spawnPosition, spawnRotation);
yield return new WaitForSeconds (spawnWait);
mobSpawned++;
if (mobSpawned <= 2)
{
yield return (true);
}
if (mobSpawned == 3)
{
yield break;
}
}
yield return new WaitForSeconds (waveWait);
}
}
}
Hello.
If is true theat code does not execute that while, is because never reach it. Is not problem of the "while".
Answer by DawidNorasDev · Jun 19, 2018 at 02:20 PM
yield break;
exits the Coroutine. So on third spawned mob, you exit from coroutine. I highly suggest to spend some time learning how to debug code through breakpoints and step by step execution. (Or for someone less experienced, placing a lot of Debug.Log("Message");
to check how the code is running)
Thanks you. I solved the problem. I added an Void Update, if mobSpawned = 0, start timer, if timer = 0 start the coroutine SpawnWaves
Your answer
Follow this Question
Related Questions
[Solved]Cant Find loop that freezes Unity 2 Answers
Why is this Do/While loop hanging Unity? 1 Answer
Do-While Loop not working 1 Answer
While loop causing Unity to hang up? 1 Answer