Spawn Code Problems Part 40,023

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!

when you dragged something into the “Enemy” slot in the inspector, did you drag it from the hierarchy panel, or the project panel. I’m betting it was from the hierarchy, and you meant to drag in the prefab from the project panel.

2 Likes

Yup, that was it! I did not even know that made a difference. Thanks for the help! I now have to figure out why my next wave is not working, but ill try to figure that out myself first.

:slight_smile: keep at it, you’ll get there

instantiate takes pretty much anything as a template. If you pass in a gameobject (or component) from the hierarchy you’re copying something within the scene, if that thing is destroyed you can’t copy it anymore because it’s “gone”. If you pass in a prefab from the project it “exists” all the time within the build (unless you jump through a few hoops to destroy the asset file… ).

1 Like