Book Image

Mastering Unity Scripting

By : Alan Thorn
Book Image

Mastering Unity Scripting

By: Alan Thorn

Overview of this book

Table of Contents (17 chapters)
Mastering Unity Scripting
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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 type at all. This makes...