- Home /
Unity Keeps Freezing on Play (Again!)
Okay so Im making a bullet hell style shooter and i have a code that spawns enemies here. Basically it has a number of enemeies to spawn, then once that number runs out it moves onto the next wave. It works spawing the first few enemies, but once the count runs out the editor crashes. I know this is becuase of an infinite loop, (Ive had this issue before,) and the "continue" and "yeild return null" parts fixed it before. However, it must be different for have a IEnumerator. Any help on how to fix it?
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class MonsterSpawner : MonoBehaviour {
public float delta = 1.5f;
public float speed = 2.0f;
private Vector3 startPos;
public Transform spawnpoint;
public GameObject shell;
public GameObject redshell;
public int EnemySpeed = 7;
public int enemiesleft = 10;
public int enemiesnext = 15;
public int wave = 1;
public bool useInvoke;
public bool running;
// Use this for initialization
void Start () {
startPos = transform.position;
if (useInvoke)
InvokeRepeating("Spawn1", 0, 3);
else
StartCoroutine(shellspawn1());
}
// Update is called once per frame
void Update () {
if (enemiesleft < 1)
{
wave = wave + 1;
if (wave == 1)
{
enemiesnext = 15;
} else if (wave == 2)
{
enemiesnext = 1;
}
enemiesleft = enemiesnext;
}
Vector3 v = startPos;
v.x += delta * Mathf.Sin(Time.time * speed);
transform.position = v;
}
void spawnshell()
{
var shellspawn = (GameObject)Instantiate(
shell,
spawnpoint.position,
spawnpoint.rotation);
shellspawn.GetComponent<Rigidbody>().velocity = shellspawn.transform.up * EnemySpeed;
Destroy(shellspawn, 10.0f);
}
IEnumerator shellspawn1()
{
running = true;
while (running)
{
if (enemiesleft > 1)
{
enemiesleft = enemiesleft - 1;
if (wave == 1)
{
spawnshell();
yield return new WaitForSeconds(0f + (Random.Range(1.5f, 3f)));
continue;
} else if (wave == 2)
{
spawnredshell();
yield return new WaitForSeconds(0f + (Random.Range(1.5f, 3f)));
continue;
}
}
}
yield return null;
}
void Spawn1()
{
if (wave == 1)
{
spawnshell();
}
else if (wave == 2)
{
spawnredshell();
}
}
void spawnredshell()
{
var shellspawnred = (GameObject)Instantiate(
redshell,
spawnpoint.position,
spawnpoint.rotation);
shellspawnred.GetComponent<Rigidbody>().velocity = shellspawnred.transform.up * EnemySpeed;
Destroy(shellspawnred, 10.0f);
}
}
Answer by bobisgod234 · Dec 12, 2017 at 12:33 AM
If "enemiesleft" is less then or equal to 1, then you end up with an infinite loop in your "shellspawn1", as the yield statements in that while loop can only be reached if enemiesleft is greater than 1.
Try adding a yield reutrn null at the very end of your while loop:
IEnumerator shellspawn1()
{
running = true;
while (running)
{
if (enemiesleft > 1)
{
enemiesleft = enemiesleft - 1;
if (wave == 1)
{
spawnshell();
yield return new WaitForSeconds(0f + (Random.Range(1.5f, 3f)));
continue;
} else if (wave == 2)
{
spawnredshell();
yield return new WaitForSeconds(0f + (Random.Range(1.5f, 3f)));
continue;
}
}
yield return null;
}
yield return null;
}
Your answer
