The question is answered, right answer was accepted
OnTriggerExit not reactivating a GameObject
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.
Answer by dskeith · Feb 27, 2017 at 11:44 PM
@Mat7itan - In your OnTriggerEnter()
method you are deactivating the spawner
GameObject. Once a GameObject is deactivated, none of its scripts or colliders run. Which is to say, OnTriggerExit()
is never called because there is never an active object to exit.
Instead of deactivating the object, you might want to deactivate the MeshRenderer
component of the object.
In OnCollisionEnter()
you should try something like:
spawner.GetComponent<MeshRenderer>().enabled = false;
And in OnCollisionExit()
:
if (other.gameObject.CompareTag("SpawnPoint1"))
{
spawner = GameObject.Find("Spawner1");
// Re-enable the renderer.
spawner.GetComponent<MeshRenderer>().enabled = true; //Error occurs here
}
Now, something to keep in mind is that by doing this - you'll only be hiding the spawner from the renderer, it will still exist. If it's a collider-body instead of a trigger - you'll run into it and be blocked by it. Also, you'll need to manage any active sounds sources, etc on your spawner object.
Reference - How to make an gameObject invisible and disappeared