Join Kelley Hecker for an in-depth discussion in this video Picking up coins, part of Unity: Scripting with C#.
- We also want our game manager…to keep track of how many coins…the player has collected.…We can then display this information on the screen.…If you're starting from the exercise files…instead of continuing from the previous video…remember that you'll need to import the standard assets…for Unity 4.6 package from the asset store…and drag in the ThirdPersonCharacter…and FreeLookCameraRig prefabs…like we did in the previous chapter.…First we need to know when the player touches a coin.…The coin object is already set up with a collider…marked as a trigger,…which you can see here in the inspector.…
Double click on the Coin class to open it.…Add the OnTriggerEnter MonoBehavior event.…OnTriggerEnter passes one argument,…which is the collider that entered the trigger.…We only want to pick up the coin…if the object that entered the trigger…is the player.…The player is already tagged with the player tag…which you can see in the inspector.…We can use the tag to tell if the object that entered…is the player or not.…
If it is the player, we call the Pickup function…
- 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.