Component interactions
We've seen the anatomy of a GameObject
as a collection of components and nothing more. This raises logistical issues about how components should interact and communicate with each other. Each component is effectively implemented as a self-contained script file, separate from any other component, yet a component must often interact with others. Specifically, you'll often need to access variables and call functions on other components on the same GameObject
, and you might even need to do this on every frame. This section explores such intercomponent communication.
One way to call functions on other components is to use SendMessage
and BroadcastMessage
, as shown in Chapter 1, Unity C# Refresher. These functions are type agnostic. Specifically, they're functions we might call anywhere in the script to invoke methods by names on all other components attached to the same object, regardless of their type. These functions don't care about the component...