Join Kelley Hecker for an in-depth discussion in this video Squashing enemies with SendMessage, part of Unity: Scripting with C#.
- Unity has a built-in function for sending messages…between objects called SendMessage.…SendMessage takes the name of the function to be called,…and can take an optional parameter…that is passed to the function.…There's also SendMessageUpwards,…which also sends the message…to all parents of the GameObject.…This is useful if a child needs to send a…message to its parent object.…There are a few reasons you would want to use…SendMessage instead of calling a function directly.…First, you can use SendMessage to access private functions.…SendMessage will also call the function on all…components of the GameObject receiving the message.…
Let's say you have several scripts…attached to a game object,…and each one has a function called OnPlayerDeath.…If you were calling this function directly,…you would have to loop through all of the scripts,…and call the function in each one.…With SendMessage, you can send the…OnPlayerDeath message to the game object,…and it will automatically call the function…in all the different scripts.…
- Defining classes and variables
- Attaching scripts to objects
- Understanding MonoBehaviour class
- Using the Unity Scripting API
- Importing assets
- Using a singleton pattern to manage game state
- Creating an event
Skill Level Intermediate
Q: Why am I getting a script compile error when trying to load the exercise files?
A: This course was recorded in Unity 4.6, and Unity has since released Unity 5. There are two items to address for Unity 5 users.
Q: Why can't I see the Render Settings option in the Edit menu?
A: The Render Settings options have been moved in Unity 5. You can access them by going to the Window menu and choosing Lighting. In the window that loads, Render Settings can be found under the Scene tab.