Join Kelley Hecker for an in-depth discussion in this video Making a coin spin, part of Unity: Scripting with C#.
- We now just need to attach our script to the coin prefab. 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 Assets Store, and drag in the Third Person Character and Free Look Camera Rig prefabs like we did earlier in this chapter. We can attach the coin script in one of two ways. First we can click on the coin prefab in the project window. Then find the coin script and drag it onto the prefab here on the right.
The other way... is to drag the prefab into the scene, and then attach the script to the coin in the scene. Again by dragging it over here to the right. You then have to hit Apply to apply these settings to the prefab object. That way when we make more coins, they all have the script instead of just the one coin we attached it to. Go ahead and place coins all around the level.
All right, so I've placed some coins all around the level. Your coins might be in different places, and that's fine. If you go ahead and hit play... you can see that the coins are spinning. But they're all spinning at exactly the same time, which looks a little weird. We can easily fix that by rotating the coins a random amount in the start function. Open the coin script.
In the start function, we're going to use the same transform.Rotate function that we used in the Spin Coroutine. For the degrees to rotate, we're going to generate a random number between zero and 360 degrees, using Random.Range. Remember that since each coin object has its own instance of the coin script, they'll each have their own random number.
That way they will all be spinning at different offsets. Go back to the editor and hit Play. And now you can see that the coins are all spinning at slightly different times. If you click on a coin, you can change its rotation speed by modifying the rotate speed variable in the inspector. Notice that this only changes the speed of a single coin.
If you want to change the speed for all the coins, hit Apply to apply the value to the prefab. You can also click on the prefab in the project window and change the value there. Any coins in the scene that you haven't set to a different speed will get the new speed from the prefab. Prefabs are useful tools when dealing with several of the same type of object, like our coins.
- 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.