O'Reilly logo

Unity 3D Game Development by Example by Ryan Henson Creighton

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required