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. ...

Get Mastering Unity Scripting now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.