---
title: "Sound Effects & Scripting"
content_type: tutorial
url: "https://learn.unity.com/tutorial/sound-effects-scripting?language=en"
language: en
difficulty: beginner
duration_minutes: 50
unity_version:
  - "4.x"
supported_unity_versions:
  - name: "4.x"
    url: "https://learn.unity.com/tutorial/sound-effects-scripting.md?language=en&version=4.x"
last_updated: "2026-08-04T14:24:53.000Z"
---

# Sound Effects & Scripting

Add event-driven sound effects in Unity with C# and work with Audio Listeners, Audio Sources, and Audio Clips.

In this recorded training session from November 2014, we demonstrate how to add sound effects to your game that are triggered by game events, using C# scripting. We also review the main audio components of Unity: Audio Listeners, Audio Sources and Audio Clips.

## 1. Sound Effects & Scripting - Live Training from November 17th, 2014

[Video](http://www.youtube.com/watch?v=5N-428vKwaE)

This script instantiates a projectile prefab and plays a sound with randomized volume.

## ThrowObject

```csharp
using UnityEngine;
using System.Collections;

public class CrashSound : MonoBehaviour {

    public AudioClip crashSoft;
    public AudioClip crashHard;


    private AudioSource source;
    private float lowPitchRange = .75F;
    private float highPitchRange = 1.5F;
    private float velToVol = .2F;
    private float velocityClipSplit = 10F;


    void Awake () {

        source = GetComponent<AudioSource>();
    }


    void OnCollisionEnter (Collision coll)
    {
        source.pitch = Random.Range (lowPitchRange,highPitchRange);
        float hitVol = coll.relativeVelocity.magnitude * velToVol;
        if (coll.relativeVelocity.magnitude < velocityClipSplit)
            source.PlayOneShot(crashSoft,hitVol);
        else 
            source.PlayOneShot(crashHard,hitVol);
    }

}
```
