Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Mar 01, 2017 at 09:09 PM by Mat7itan for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Mat7itan · Feb 27, 2017 at 09:59 PM · c#setactiveontriggerexit

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

314 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unable to reactivate an object that's been deactivated. C# 4 Answers

SetActive(true) doesn't seem to be working 1 Answer

SetActive dosen't work 0 Answers

Set object from array to active only whilst a specific number is returned 2 Answers

How to save setactive in array 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges