Can you create a custom 'OnEvent()' function?
Hey.
I'd like to know if it's possible to create a custom event function that would be called on all the scripts it is declared once something happens.
Like this:
You have several scripts that will behave differently at a single event. Let's say for when the player dies. You would declare OnPlayerDeath() inside all those scripts and make them do their specific routines.
When the player dies, OnPlayerDeath() is automatically called on all the scripts where it is declared.
That way you would not need to reference all those possibly dozens of different scripts inside the Player script, nor use Update() on every instance of those different scripts to constantly check if(playerIsDead) and consume unnecessary CPU processing power.
Is there a way one can do that? If not in this specific way, what would be the best approach?
Thank you.
Answer by Landern · Aug 24, 2016 at 02:05 PM
Yes you can, each script/object would need to subscribe to the event, unsubscribe when necessary. This isn't unity specific but implementation in c#.
And on and on.
Your answer
![](https://koobas.hobune.stream/wayback/20220612091435im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
ScrollBar and scrollrect seem not working on mobile 0 Answers
IPointerEnterHandler calls immediately after IPointerExitHandler 0 Answers
Different approach to an upgrade system 4 Answers
UnityEvent NullreferenceException in inspector 0 Answers
How do I make a Status Effect system in a turn based combat RPG ? 0 Answers