Time for action - add sounds to the FallingObjectScript

Let's drop a couple of lines into the FallingObjectScript to fire off a few sounds:

  1. Open the FallingObjectScript.
  2. Declare a clip1 variable at the top of the script:
    var prefab:GameObject;
    var clip1:AudioClip;
    
  3. Just after we determine that the falling object has hit the ground, play the clip1 sound:
    if(transform.position.y < 0)
    {
    audio.PlayOneShot(clip1);
    

By now, you've probably guessed that there's one more step involved. The script really has no idea what clip1 actually is it's just a variable placeholder. Let's add some actual sound files to the Bomb and Stein prefabs to get this party started:

  1. In the Project panel, click to open the SFX folder. Yay sounds!!
  2. Again in the Project panel, click ...

Get Unity 3D Game Development by Example 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.