So I have this enemy object that destroys itself upon colliding with my player game object. After doing so, I refer to the following script for what to do afterwards:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Respawn : MonoBehaviour {
public GameObject enemy;
public float respawnTimer;
public float noCount;
public Transform[] SpawnPoints;
void Start(){
noCount = 1;
respawnTimer = 3;
}
// Update is called once per frame
void Update () {
//Spawning and Respawning
if (noCount < 1) {
respawnTimer -= Time.deltaTime;
}
if (respawnTimer <= 0) {
respawnTimer = 3;
Spawn ();
}
}
void Spawn()
{
if (noCount < 1)
{
int spawnIndex = Random.Range (0, SpawnPoints.Length);
GameObject clone = Instantiate (enemy, SpawnPoints [spawnIndex].position, SpawnPoints [spawnIndex].rotation) as GameObject;
noCount += 1;
}
}
}
With this script, I’ve been getting an error saying that I’m referring to a destroyed game object, however, last I checked, I set “enemy” as a prefab in the inspector, so shouldn’t this be working? Or am I dumb and I need to do something else in the script?
As a side note, don’t worry about the noCount variable, it does indeed become less than one, but that is done via another script.