Hello everyone. I am trying to make a simple game with spawners etc. and recently after altering some code, Unity seems to freeze completely. Not only the game but the editor too. Could anyone tell me what’s the problem?
using System.Collections;
using UnityEngine;
public class Spawner : MonoBehaviour
{
//public AudioClip DieSoundeffect;
//public GameObject explosion;
public GameObject[] enemies;
public Vector3 spawnValues;
public float spawnWait;
public float spawnMostWait;
public float spawnLeastWait;
public int startWait;
public int rendEnemy;
public int kill = 0;
public int notkill = 0;
public int count;
public int KillCounter;
public int KillMax;
public int KillLeast;
public int counter_max;
private int kill_max_spawn = 5;
private int notkill_max_spawn = 3;
// Start is called before the first frame update
void Start()
{
StartCoroutine(WaitSpawner());
while (kill < kill_max_spawn)
{
Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), 1, Random.Range(-spawnValues.z, spawnValues.z));
Instantiate(enemies[0], spawnPosition + transform.TransformPoint(0, 0, 0), gameObject.transform.rotation);
kill++;
KillCounter++;
}
while (notkill < notkill_max_spawn)
{
Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), 1, Random.Range(-spawnValues.z, spawnValues.z));
Instantiate(enemies[1], spawnPosition + transform.TransformPoint(0, 0, 0), gameObject.transform.rotation);
notkill++;
KillCounter++;
}
}
// Update is called once per frame
void Update()
{
spawnWait = Random.Range(spawnLeastWait, spawnMostWait);
}
IEnumerator WaitSpawner()
{
yield return new WaitForSeconds(startWait);
while(true)
{
count = Random.Range(KillLeast, KillMax);//8,9 notkill - 1, kill - 0
while (KillCounter < counter_max)
{
rendEnemy = Random.Range(0, 2); //0,1
if (notkill >= kill)
{
rendEnemy = 0;
}
if (kill >= count)
{
//rendEnemy = 1;
}
Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), 1, Random.Range(-spawnValues.z, spawnValues.z));
Instantiate(enemies[rendEnemy], spawnPosition + transform.TransformPoint(0, 0, 0), gameObject.transform.rotation);
KillCounter++;
if (rendEnemy == 0)
{
kill++;
}
else
{
notkill++;
}
yield return new WaitForSeconds(spawnWait);
}
}
}
}