How to play an Audio clip in Unity

Sounds are very important in a game if a sound effect is played at the wrong time, it can ruin the whole gameplay experience. Unity has many tricks to play sound in synchronous with your gameplay. In this tutorial we will see how to play an audio clip and how to synchronize it with your gameplay.

How Play audio clip on scene start

  1. Create a new Empty object in hierarchy window by clicking Create>Create Empty.
  2. Go to the inspector window and click on Add Component.
  3. Search for AudioSource and click Add
  4. Drag and drop your audio file to the AudioClip dialog box under AudioSource in the inspector window.
  5. Check Play On Awake to play the audio when the scene loads.
  6. Check Loop option if the audio is required to play continuously.
Unity play audio inspector settings

How to play Audio clip on Trigger

  1. Create a new script called Play_audio.
  2. Copy and paste the code below.
  3. Attach your script to the gameobject with the audio source.
  4. Assign your clip to the source.
  5. Disable Play on Awake in the audio source.
  6. Play the game and the audio clip will play when the condition in the script is met.

Below script uses time delay as the condition. You can add your required condition to the script.

public class Play_audio : MonoBehaviour
{
    bool ready=false;
    float delay=5f;
    AudioSource aud;
    bool audioplayed=false;
    // Start is called before the first frame update
    void Start()
    {
        aud=GetComponent<AudioSource>();
        StartCoroutine("Delaythis");
        
    }
    IEnumerator Delaythis()
    {
    
        yield return new WaitForSeconds(delay);
        ready=true;


    }

    // Update is called once per frame
    void Update()
    {
        if(ready && !audioplayed)
        {
            aud.Play();
            audioplayed=true;
        }
        
    }
}

How to play an Audio clip at point

  1. Create a new script called play_at_point.
  2. Copy and paste the below code to the script.
  3. Add the script to any gameobject in scene.
  4. Assign audio clip to the script in the inspector window.
  5. Assign the position to at which the clip has to be played.
  6. Play the game and the audio clip will play at the assigned point.
using UnityEngine;

public class play_at_point : MonoBehaviour
{
    public Vector3 point;
    public AudioClip aud;

    void Start()
    {
        AudioSource.PlayClipAtPoint(aud,point);
    }
}

How to play an audio clip without audio source

  1. Create a new script.
  2. Copy and paste the code from play audio clip at point section.
  3. Attach the scrip to your gameobject.
  4. Set position to (0,0,0).
  5. Press play and the audio will play without an audio source.

Disadvantages of playing audio clip without an audio source

  1. You cannot control the audio play settings.
  2. You cannot stop the audio.
  3. No control on sound effects.
  4. Audio will be looped continuously.

How to play audio clips in sequence in Unity

Unity play audio in sequence inspector settings
  1. Create a new script.
  2. Copy and paste the below code on to the script.
  3. Attach script and AudioSource to an empty game object.
  4. Uncheck Play on Awake.
  5. Assign the number of clips to the public variable in the inspector.
  6. Drag and drop your clips to the script.
  7. Now all your audio clips will play in sequence.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Threading.Tasks;

public class Play_audio : MonoBehaviour
{
    public Vector3 point;
    public AudioClip[] Clip;
    AudioSource aud;
    float delay_time=0f;


    async void Start()
    {
                
        aud=GetComponent<AudioSource>();
        foreach(AudioClip n in Clip)
        {                      
            await Task.Delay((int)delay_time*1000); //Task.Delay input is in milliseconds
            playaudio(n);   
            
        }
    }

    void playaudio(AudioClip n)
    {     
        aud.clip=n;
        aud.Play();
        delay_time= n.length+1;//1 second is added to cater for the loading delay          

    }
}

Above script uses async and task.delay to play the audio at a time difference.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.