- Home /
Why is this IEnumerator instantly crashing my game?
IEnumerator Dungeon() {
int X = Random.Range (-3, 3)*2;
int Y = Random.Range (-2, 2)*2;
Vector2 pos = new Vector2 (X, Y);
if (Vlist.Contains (pos) == true) {
print ("repeated");
yield return Dungeon (); //this is what crashes it
}
Vlist.Add (pos);
if (i == Rooms)
StopCoroutine (Dungeon ());
else {
i++;
Instantiate (Room, pos, transform.rotation);
yield return Dungeon();
}
}
Essentially what it's supposed to do is randomly generate a dungeon map. It works pretty well until i added the vector list that was supposed to stop it from repeating tiles but instead it instantly crashes my game. Is it only because it causes an "infinite" loop by accident? Or is it something else?
thanks for your time <33
Comment
Best Answer
Answer by IgorAherne · Jun 08, 2017 at 08:46 PM
It definitely falls into recursion
By the way, you shouldn't call coroutine like Dungeon()
use StartCoroutine()
instead
for example yield return Dungeon()
is incorrect