So Sam does a lot of event bindings in Start methods without unbinding it later on (not neccesarily in this course).
From what i know common practice is to bind them in OnEnable and then unbind them in OnDisable.
Does it matter? Does skipping unbinding not lead to memory leaks?
If the publisher outlives the subscriber, yes, it might cause memory leaks, but if it doesn’t, then no, there won’t be any memory leaks whatsoever.
Basically you want to be sure to unsubscribe if the publisher is a singleton, also if the subscriber will get destroyed or disabled before the publisher, otherwise it will not only have a memory leak but an error, your game simply won’t work, that’s why is common practice to unsubscribe, but it’s not 100% necessary.
In the topic of event unbinding - do lambdas unbind themself or they need to have action taken on them somehow.
Lets say i do
//class 1
public class Foo
{
public static event Action onAddThings;
}
//class 2
public class Bar
{
int i = 0;
void OnEnable() => Foo.onAddThings += () => ++i;
}
and lets say i enable and disable object 20 times. Will there be only one event and i would increase only once?
I think it will be if you need to remove the listener again because you have no reference to the lambda, and you need the reference to remove it. Same as events
This wasn’t a problem because the button will be destroyed and the Lambda will just go away with the button. If, however, you were pooling, the Lambda is not the way to go.