How to play some audio and sound files by clicking some buttons respectively in Unity



Create a script in your Unity editor and name it MyAudioManager.cs

Then paste this code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MyAudioManager : MonoBehaviour
{
	
	public AudioClip one;
	public AudioClip two;
	public AudioClip three;
	
	AudioSource audio;
	
    // Start is called before the first frame update
    void Start()
    {
        audio = GetComponent<AudioSource>();
    }

    
	public void PlaySound(string audioName){
		switch(audioName){
			case "one" :
				audio.clip = one;
				break;
			case "two" :
				audio.clip = two;
				break;
			case "three" :
				audio.clip = three;
				break;
		}
		audio.Play();
	}
	
}

And watch the video for more steps:

Then you will know how to play multiple audio sound files by clicking some buttons on Unity 😀

loading...

Leave a Reply

Your email address will not be published.