Reputation: 13
In the game I am making, I want to Re-Activate a gameObject that has been set to Inactive using (SetActive(false)) once it hits a trigger. My method doesn't work. My code-
How to solve this problem. ?
private void OnTriggerExit(Collider coin)
{
if (coin.CompareTag("coinHolder"))
{
coin.SetActive(true);
Debug.Log(coin.name);
}
}```
Upvotes: 0
Views: 3147
Reputation: 2340
Once you deactivate your game object (̶o̶r̶ ̶c̶o̶m̶p̶o̶n̶e̶n̶t̶)̶, no longer messages are sent to this object!
So methods like:
will no longer be called.
EDIT: As @derHugo mentioned in the comment. OnTriggerExit will be called if you disabled Component, but if you disabled GameObject it will not! This is because you disabled Collider (with the game object) which will not result in OnTriggerExit;
For this to work, you need to separate logic form display, and only disable renderer (mesh renderer or image), and leave your component (mono behavior) enabled!
Upvotes: 1
Reputation: 330
Unity life cycle methods are not called when a game object is disabled. To overcome this problem, re-arrange your game object hierarchy so that the part that uses this function is always active. For example:
Main object (Always active)
├── Visible object (Deactivatable)
└── Collider with main logic (Deactivatable)
└── Non-visible object (Always active)
└── Dummy collider (Always active) (that would trigger OnTriggerExit)
Upvotes: 1