- Home /
player objects needs to broadcast its movement all enemies
The problem: I have a player object which always moves the same fixed distance when the user presses a key. Other GameObjects (different enemy types) should do stuff depending on the percentage of the (fixed) distance the player has traveled. These percentage are fixed values: 0% (start moving), 25%, 50%, 75% and 100% (done moving).
A possible solution i'm thinking of:
1. register the different enemy types with the game controller
2. player movement script calls methods of the game controller at the specified distances when moving.
3. the methods on the game controller called by the player movement script, loop through the registered enemies and call methods on these enemies (enemies could implement an interface to ensure they have the correct methods)
Everything now depends on the game controller, and it seems overly messy and totaly wrong to do it this way...
Is there a better solution to this problem, maybe some sort of broadcasting or event system?
Answer by wilco79 · Apr 22, 2016 at 10:25 AM
For everyone facing the same problems, C# Events seem to be a solution.
Read up on the following:
http://forum.unity3d.com/threads/using-c-events-with-unity.58367/
https://msdn.microsoft.com/en-us/library/aa645739(v=vs.71).aspx
http://forum.unity3d.com/threads/the-observer-design-pattern.290376/
Or use the ready to use out of the box "Advanced CSharp Messenger":
Your answer
![](https://koobas.hobune.stream/wayback/20220612075303im_/https://answers.unity.com/themes/thub/images/avi.jpg)