I want to make a main Gameobject in Unity3D that would act as a command center for anything related to controls so that any component in the game that needs to know if the player is trying to go right would get a notification or a state change and act accordingly. I am kind of new to programming and scripting in Unity, so I know this is possible, but I don’t know how to do it specifically with the engine.
A few concepts I came across are:
- state changing techniques
- Singleton God classes
- Parent GameObject with children that send messages upwards and receive them downwards
- Inheritance of abstract classes that act as interfaces and get implemented into some collection and iterated through to get the message called on
If you guys have any insights on this subject that would be great. I basically want to find out what’s a good approach for this problem, how to decide on a good one (Pros vs. Cons) or if I got it all wrong. 🙂
All I am looking for is an implementation example of your method of choice that worked for this purpose. No need to go into each separate method. Maybe a brief description of why you decided on your pattern would be great!
Thanks in advance!
Well, you may use a C# events:
1) Create your CommandCenter class (MonoBehaviour), implement there some events you need to share, and make some public methods to call tham.
2) whenever you handle a button player press, call the method in CommandCenter, that calls an event (or, you may just make events public and call tham directly)
3) In each component, witch should know about some event, in
Awakemethod, do the following:4) don’t forget to unsubscribe for events on destroy:
5) So, in
localMyEventHandlermethod you can handle this event.