How do you understand the Observer Pattern?

Hello, i am new in unity und took this course as my starting-point and it is working great for me!
(My native language is not english, so please excuse this and future mistakes ;-))

Sorry but im missing something like:

    void OnDestroy()
    {
        GetComponent<PlayableDirector>().stopped -= EnableControl;
        GetComponent<PlayableDirector>().played -= DisableControl;
    }

I don`t think, that its save to let the observers stay in the list and not remove them.
Or is there something I overlook here.

1 Like

You certainly can put that in there, if you wish. The component isn’t one that you’ll be activating and deactivating a lot, and in fact since the only source of the PlayableDirector on this GameObject being played is this script, when OnDestroy() is called, the PlayableDirector will likely be destroyed as well.

All of that being said, it would be considered Best Practice to disable subscriptions, either in OnDestroy() or onDisable(), depending on whether the subscription is added in Awake/Start or OnEnable.

1 Like

I fully agree with you, it’s not really necessary here.
The video “The Observer Pattern In C#” did not discuss this topic and that was the reason for the request. Thank you very much!

Privacy & Terms