Usually yes, AFAIK if GameObject is destroyed, but you didnât unsubscribe from event, then this event will have reference to subscribed object and GC wouldnât be able to remove it, i.e. this will be a memory leak.
Also unsubscription makes sense in context of Unity, all of your Awake/Start/Update functions donât work on disabled scripts or on disabled GameObjects, all unity components donât work as well, so it makes sense to subscribe in OnEnable and unsubscribe in OnDisabled to make disabled objects purely disabled.
It depends on which object youâre talking about.
Basically an event system is the observer pattern (with a different interface). So you have an observer (the thing that is listening for an event), and the subject (the thing that has an event).
When you register an event handler with the subject, the subject now has a reference to the observer (itâs a delegate, but the delegate has a reference to the observer since itâs needed to call the method in question).
This means if the observer is destroyed there is a reference still out there in the wild pointing at it which keeps the GC from cleaning it up. So when an objserver is destroyed it should unregister all event handlers. (note that static event handlers donât matter here since there is no instanced object to reference back to).
Vice versa doesnât actually need unregistering since the observer doesnât actually maintain any reference to the subject (unless you explicitly did so with some variable⌠in which case you just need to set that variable null).
That all goes for C# events/delegates.
When it comes to UnityEvent things get a bit more complicated.
There are 2 ways to register with a UnityEvent. There are âpersistent callbacksâ and regular old listeners. The persistent ones are the callbacks you register through the inspector, and regular ones are done through âAddListenerâ.
AddListener approach really just is a delegate so really the rules that are applied to C# events are the same. Observerâs need to unregister themselves when destroyed otherwise the subject will hold a ref to them.
The persistent though⌠:shrug: on the specifics. You can sift through the source code to try and glean the specifics:
But it has all sorts of fun stuff going on in it. At first glance it appears like it would hold a reference, but there might be some unity engine side stuff that cleans it up. Could run some tests to find out⌠but Iâm at work right now. Maybe later today when I have a longer break.