playing audio in unity by C# script

 Audio in unity are played with the help of AudioSource component and you can access it by script and control play, pause, and stop operations. 
  • First create a empty gameObject and add AudioSource component in it. then drag and drop you music clip in it.
  • AudioSource component have various other options you can customize according to you needs.
  • It have loop option if it toggled on then our AudioClip will repeat again and again when finish. so if you want to play only once then you can turn off but if you want want to play continuously then turn on.
  • It also have play on awake toggle default turn on. so turn it off if you don't want to play music as soon as game start.

playing music in unity by script




  • Create a C# script and create a other empty gameObject in inspector and drag and drop this script into gameObject.
  • Now open script and declare a public GameObject of name "audio_object" for refencing gameObject have audioAource and an private AudioSource named "my_audio" for accessing AudioSource ccomponent.
playing audio in unity


  • Also declare a bool named "is_playing" to keep track of audioSouce is playing or not and intialize as false.
  • Now get AudioSouce by using GetComponent method in start function.
  • After that you can call my_audio.Play() to play audioClip, my_audio.Pause() to pause and my_audio.Stop() to stop audioClip.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class test : MonoBehaviour
{
    public GameObject audio_object;

    private AudioSource my_audio;
    bool is_playing=false;
    // Start is called before the first frame update
    void Start()
    {
       my_audio = audio_object.GetComponent<AudioSource>();
    }

    void Update()
    {
        if(Input.GetKeyDown(KeyCode.P) && !is_playing)
        {
            my_audio.Play();
            is_playing=true;
        }

        if(Input.GetKeyDown(KeyCode.S) && is_playing)
        {
            my_audio.Stop();
            is_playing=false;
        }
    }
}


  • in update method we have used if(Input.GetKeyDown(KeyCode.P) && !is_playing) so if we press 'p' in keyboard and audio is not playing then  my_audio.Play(); play audioClip and is_playing=true; so if press 'p' key again then it will not start over again and again.
  • secondly for stopping audioClip if(Input.GetKeyDown(KeyCode.S) && is_playing)  so if press 's' key and audioClip is playing then we call my_audio.Stop(); code to stop audioClip and  is_playing=false; so it not run again.
  • if you want to play and pause and not want start from beginning then you can use my_audio.Pause(); to pause and next time you call my_audio.Play();  it start from where it paused.
All above are for reference so you can use then according to your game needs and whenever you want to play, pause or stop audio clip you can call AudioSource.Play(), AudioSource.Pause, or AudioSource.Stop().

Always take care that gameObject must have an AudioSource and AudioSource must have an AudioClip in it.


Popular posts from this blog

Positioning GameObjects in unity editor

Creating count down timer in unity by c# script

Changing Image in unity by C# script