Adding Celebration Sounds
The only missing sound at this point is the success sound when an animal makes it to the barn.
To add those:
OnCollisionEnter
should look like this when you’re finished:
This tells the barn that, when an animal collides with it, it should get the AudioSource component and play it. Save your changes, then click play and launch animals into the barn. A success sound will now play when the animal makes it to the barn.
- Start by clicking on the barn object in the Hierarchy.
- In the Inspector, add an AudioSource component to it.
- Then, drag the win sound from Assets/RW/Sounds to the AudioClip field.
- This time, make sure Play On Awake is turned off. This sound will only be played if an animal makes to the barn.
- Double-click on the Barn script in Assets/RW/Scripts.
- Underneath the opening brace of the
if statement
, add the following:
GetComponent<AudioSource>().Play();
OnCollisionEnter
should look like this when you’re finished:
void OnCollisionEnter(Collision col)
{
if(col.gameObject.GetComponent<Animal>())
{
GetComponent<AudioSource>().Play();
Destroy(col.gameObject);
}
}
This tells the barn that, when an animal collides with it, it should get the AudioSource component and play it. Save your changes, then click play and launch animals into the barn. A success sound will now play when the animal makes it to the barn.
GetComponent<AudioSource>().Play();
void OnCollisionEnter(Collision col)
{
if(col.gameObject.GetComponent<Animal>())
{
GetComponent<AudioSource>().Play();
Destroy(col.gameObject);
}
}
![](https://koenig-media.raywenderlich.com/uploads/2016/05/mkit.png)
Success!
Finishing Touches: Varying Sound Effects
There’s one last bit of polish you can add for very little effort. Hearing the exact same sound over and over is annoying. To keep sounds fresher and less irritating, code in some variance to the pitch.
Open the Animal C# script in the Scripts folder. Add the following just under public bool isDead = false;
void Start()
{
AudioSource audioSource = GetComponent<AudioSource>();
audioSource.pitch = Random.Range(0.8f, 1.5f);
audioSource.Play();
}
This code fetches the AudioSource component and sets the pitch variable to a random value between 0.8 and 1.5. Now, click Play and launch the animals. Their animal sounds will vary each time. Pretty cool, right?
![](https://koenig-media.raywenderlich.com/uploads/2016/05/baa.png)
Where to Go From Here?
Great job adding your first sound effects to a Unity game! Now, your animals can happily moo, oink and baa their way into the barn — unless they get hit by a tractor, of course.
If you ran into any issues, open the final project to compare against using the Download Materials button at the top or bottom of this tutorial.
For more fun, try adding different pitches to each of the tractors, to the success sound that plays when an animal makes it to the barn, and to the death sounds. You can also try adding different animal sounds and a unique death sound for each animal (although that will require a little more coding, along with finding more audio files).
To learn more about Unity’s sound engine, check out Unity’s Live Training on Adding Sounds Effects to Your Game. Also, check out the AudioSource and AudioClip Unity documentation.
Stay tuned for future Unity tutorials! Feel free to leave comments below.