Every time my game runs and I destroy one of my enemy sprites, the game does not spawn additional enemies and gives me this error:
MissingReferenceException: The object of type ‘GameObject’ has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
Then is says the following: EnemySpawner.Update () (at Assets/Scripts/EnemySpawner.cs:32)
(I think this is the location of my error)
I can post any other pieces of code if needed
Code:
using System.Collections;
public class EnemySpawner : MonoBehaviour {
public GameObject EnemyPrefab;
float SpawnDistance = 15;
float EnemyRate = 5;
float nextEnemy = 1;
// Update is called once per frame
void Update () {
nextEnemy -= Time.deltaTime;
if(nextEnemy <= 0) {
nextEnemy = EnemyRate;
EnemyRate *= 1.25f;
if(EnemyRate < 2) {
EnemyRate = 2;
}
for(int i = 0; i < EnemyPrefab.GetLength(0); i++)
{
Vector3 offset = Random.onUnitSphere;
offset.z = 0;
offset = offset.normalized * SpawnDistance;
GameObject go = (GameObject) Instantiate(EnemyPrefab*, transform.position + offset, Quaternion.identity);*
_ EnemyPrefab = go;_
* }*
* }*
* }*
}