- Home /
Unity Events - Subscribe and Unsubscribe Odd Behavor
I am sending events based on the following tutorial
My code is set up so a key press fires an event that is handled by a script. That script is a child gameobject of a parent panel. The script sets the SetActive property on the parent panel through the ToggleSelfActive function.
In my script I subscribe/unsubscribe to the event as shown
void OnEnable(){
InputEventMapper.MainMenu += ToggleSelfActive;
}//end OnEnable
void OnDisable(){
InputEventMapper.MainMenu -= ToggleSelfActive;
}//end OnEnable
Now, on the screen I have another button in a different panel that will set my original panel to inactive (though the same ToggleSelfActive function). When that happens the OnDisable in my script get called (I added some logs to verify this). At that point I would expect my original key press event to no longer work. However, it turns out it still does.
I ran some tests and it appears that the unsubscribe never happens no matter what I do. Even if I tell it to unsubscribe by some other independent function, it never seems to do so. This is actually also true for subscribe. If I move the subscribe code to some other function in my script that I call by some external means, the MainMenu event never seems to be subscribed to.
Any ideas?
Well, in an effort to provide more information in hopes that someone might know what is happening, I ran a few more tests.
I isolated my condition to a separate control for subscribing and unsubscribing. So basically I have one gameobject that subscribes and unsubscribes from an function which I can call externally.
Basically unsubscribe seems to do absolutely nothing. This doesn't make sense. Surely there is something I am missing here.