Time for action – add sounds to FallingObject

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

  1. Open the FallingObject script.
  2. Declare a clip1 variable at the top of the script:
      var prefab:GameObject;
      var speed:int;
      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, ...

Get Unity 4.x Game Development by Example Beginner's Guide 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.