I have been trying to get this spawn code for my space shooter game to work for a while now, and I have now encountered my latest problem! I get this error code,
“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.”
The whole error code is,
“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.
UnityEngine.Object.Internal_InstantiateSingle (UnityEngine.Object data, Vector3 pos, Quaternion rot)
UnityEngine.Object.Instantiate (UnityEngine.Object original, Vector3 position, Quaternion rotation)
Spawn.Update () (at Assets/Scripts/Spawn.cs:37)”
The code relating to this is,
using UnityEngine;
using System.Collections;
public class Spawn : MonoBehaviour
{
public Wave[] waves;
public GameObject [] spawnpoints;
public GameObject Enemy;
Wave currentWave;
int currentWaveNumber;
int enemiesRemainingToSpawn;
int enemiesRemainingAlive;
float nextSpawnTime;
[System.Serializable]
public class Wave
{
public int enemyCount;
public float timeBetweenSpawns;
}
void Start() {
NextWave ();
}
void Update() {
if (enemiesRemainingToSpawn > 0 && Time.time > nextSpawnTime) {
enemiesRemainingToSpawn--;
nextSpawnTime = Time.time + currentWave.timeBetweenSpawns;
spawnpoints = GameObject.FindGameObjectsWithTag ("Spawnpoint");
GameObject spawn = spawnpoints [Random.Range (0, spawnpoints.Length)];
Instantiate (Enemy, spawn.transform.position, spawn.transform.rotation);
}
}
void NextWave()
{
currentWaveNumber++;
print ("Wave: " + currentWaveNumber);
if (currentWaveNumber - 1 < waves.Length)
{
currentWave = waves [currentWaveNumber - 1];
enemiesRemainingToSpawn = currentWave.enemyCount;
enemiesRemainingAlive = enemiesRemainingToSpawn;
}
}
public void OnEnemyDeath()
{
enemiesRemainingAlive --;
if (enemiesRemainingAlive == 0)
{
NextWave();
}
}
}
And lastly, a screenshot of my game is below.
I know this is a lot of information to sort through, but if anyone could help me piece this together I would greatly appreciate it. If anyone needs any more info let me know and thanks for the help!