OBSERVER Pattern
Objective: OBSERVER Pattern.
Observer pattern is the design pattern where object create a notify behavior/event , then observer(Object want to be notified) registered to this event, then the observer decide want to do when get notified.
Note: Read again to understand this definition after reading up to the end the article.
Before start to do this create 4 script, a Player , Crocodile, Dragon, Dog .Then create three renaming them Crocodile, Dragon, Dog and attach their script.
Go to the player which will be the subject create a delegate of type action. Then listen then check if any one registered to this delegate, then call that delegate.
Note: Read my article about delegate if you don't know how delegate work, plus you can use any delegate type.
Or We can do like this
Now go to the subscribers, registered to the method you want/to what you want to happen in that method.
Repeat this to other remaining script, then assign different color to those objects in their method.
Important!!
Make sure you cache the material with method which will be called before disable to avoid Error, check Method Order of Execution.
Link to official documentation https://docs.unity3d.com/Manual/execution-order.html
Now need to unsubscribe or unregistered to this event, which is the best practice when use delegate. Repeat this to other observer.
Last attach the player script to the game object for the sake of this article I will attach it to the main camera.
This is how to use Observer Pattern.
Why use Observer pattern?
1.Objects don't need to know about each other?
2.Remove class Independent from other class