- Home /
Deactivating object makes events null. What is the best workaround?
public delegate void HandworkAction();
public event HandworkAction grab;
After deactivating the object that contains the script, and reactivating it again, events become null, causing nullreference exception. Is this by any change a bug, why does this happen and what do you recommend me to do?
Answer by Roktom · Nov 21, 2016 at 07:40 PM
I am not an event guru , but that's how I would do it :
Subscribe to event during OnEnable().
Unsubscribe from event during OnDisable().
before invoking event check if it has subscribers.
Or use UnityEvent rather than a normal event, which should be easier and much more bug free. Also I would define events as public static in a class made specifically for it.
Your answer
Follow this Question
Related Questions
Question on Events and Delegates 1 Answer
Unity Event Inspector Condition 0 Answers
How to change a Player direction by colliding a Game Object 1 Answer
Raise and Event with EventMessenger 0 Answers
How can I detect Keyboard Events? 4 Answers