I am working on a game in which the player has to search a maze to collect a certain number of keys to open a door that leads to the exit. They have to fight through enemies that continuously spawn from spawn points located within the maze. Originally I had a trigger collider set to the actual spawn point, and when the player entered the collider the spawn point would be disabled. The problem with this was that the collider was also disabled and would not detect if the player left the area of the collider.
I changed this so that the actual spawner is a child of the Game Object that contains the collider so that when the player enters the trigger the child object is disabled, and the collider remains enabled. However, upon exiting the area of the collider the game crashes and the console displays: NullReferenceException: Object reference not set to an instance of an object. I don’t know if I am just using OnTriggerExit correctly (it is my first time using it), or if when an object is inactive is can no longer be referenced. I am really new to unity still (I’ve only been using it for about 2 months and I havne’t been working with it often), so am I just using OnTriggerExit wrong, can you not reference a deactivated object, or is it something I am missing altogether?
Code that controls the deactivation/activation of the spawn points:
GameObject spawner;
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag(“SpawnPoint1”))
{
spawner = GameObject.Find(“Spawner1”);
spawner.SetActive(false);
}
if (other.gameObject.CompareTag(“SpawnPoint2”))
{
spawner = GameObject.Find(“Spawner2”);
spawner.SetActive(false);
}
if (other.gameObject.CompareTag(“SpawnPoint3”))
{
spawner = GameObject.Find(“Spawner3”);
spawner.SetActive(false);
}
if (other.gameObject.CompareTag(“SpawnPoint4”))
{
spawner = GameObject.Find(“Spawner4”);
spawner.SetActive(false);
}
}
private void OnTriggerExit(Collider other)
{
if (other.gameObject.CompareTag("SpawnPoint1"))
{
spawner = GameObject.Find("Spawner1");
spawner.SetActive(true); //Error occurs here
}
if (other.gameObject.CompareTag("SpawnPoint2"))
{
spawner = GameObject.Find("Spawner2");
spawner.SetActive(true); //Error occurs here
}
if (other.gameObject.CompareTag("SpawnPoint3"))
{
spawner = GameObject.Find("Spawner3");
spawner.SetActive(true); //Error occurs here
}
if (other.gameObject.CompareTag("SpawnPoint4"))
{
spawner = GameObject.Find("Spawner4");
spawner.SetActive(true); //Error occurs here
}
}
Code that controls spawing (if it’s relevant):
public class EnemyManager : MonoBehaviour
{
public PlayerHealth playerHealth;
public GameObject enemy;
public Transform spawnPoints;
public float spawnTime = 3f;
public int enemyCount = 0;
public int EnemyCount
{
get { return enemyCount; }
set { enemyCount -= value; }
}
void Start ()
{
InvokeRepeating ("Spawn", spawnTime, spawnTime);
}
void Spawn ()
{
if(playerHealth.currentHealth <= 0f)
{
return;
}
if(enemyCount <= 50)
{
int spawnPointIndex = Random.Range(0, spawnPoints.Length);
Instantiate(enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
}
}
}
No idea why some of the code is formatted and the rest isn’t.