On Button Click Start The Audio Using C# Scripts In Unity

Introduction

 
This article demonstrates how to play audio with a button click using C# scripts in Unity.
 
Prerequisites
 
Unity Environment version 2018.4.19f1
 

Create a New Project

 
On Button Click Start The Audio Using C# Scripts In Unity
 

Create the Button

 
if a canvas is already present in the Hierarchy, right click the canvas and select UI > Button.
 
On Button Click Start The Audio Using C# Scripts In Unity
 
Select the button on click (Ctrl + D). Create a Duplicate Button.
 
On Button Click Start The Audio Using C# Scripts In Unity
 
Select Button. GetComponentInChildren<Text>(). text = " button text" as Play, Pause and Stop in scene view.
 
On Button Click Start The Audio Using C# Scripts In Unity
 
Google Search text to speech Create the speech download.
 
On Button Click Start The Audio Using C# Scripts In Unity
 
Import the Audio in scene view.
 
On Button Click Start The Audio Using C# Scripts In Unity
 
Rename the download Audio Play, Pause and Stop.
 
On Button Click Start The Audio Using C# Scripts In Unity
 

Create the Scripts

 
Right-click on Assets. Select Create >> C# script
 
On Button Click Start The Audio Using C# Scripts In Unity
 
Rename the Scripts as Musicmgr.
 
On Button Click Start The Audio Using C# Scripts In Unity
 
Double click on the Musicmgr script. Write the code as shown below,
  1. using System.Collections;  
  2. using System.Collections.Generic;  
  3. using UnityEngine;  
  4. public class Musicmgr: MonoBehaviour {  
  5.     public AudioSource playaudio;  
  6.     public AudioSource pauseaudio;  
  7.     public AudioSource Stopaudio;  
  8.     // Start is called before the first frame update      
  9.     void Start() {}  
  10.     // Update is called once per frame      
  11.     void Update() {}  
  12.     public void PlayMusic() {  
  13.         playaudio.Play();  
  14.         Debug.Log("play");  
  15.     }  
  16.     public void PauseMusic() {  
  17.         pauseaudio.Pause();  
  18.         Debug.Log("pause");  
  19.     }  
  20.     public void StopMusic() {  
  21.         Stopaudio.Stop();  
  22.         Debug.Log("stop");  
  23.     }  
  24. }  
Save the program
 

Create the Empty gameobject

 
Create a empty game object (GameObject->Create Empty)
 
On Button Click Start The Audio Using C# Scripts In Unity
 
Rename the Empty GameObject as Musicmgr
 
On Button Click Start The Audio Using C# Scripts In Unity
 
Drag and drop the Musicmgr script onto the Musicmgr
 
On Button Click Start The Audio Using C# Scripts In Unity
 
GameObject selected in the inspector, click Add Component.You can search for Audio Source and select this. Assign your AudioClip to the Audio Source.
 
On Button Click Start The Audio Using C# Scripts In Unity
 
Drag n Drop Audio source to musicmgr
 
On Button Click Start The Audio Using C# Scripts In Unity
 
Add an audio source Musicgmgr to your scene.Assign an audio clip to it.Uncheck "Play on Awake"Go to the OnClick() section of the UI
 
Button.Click the plus sign to add an item to the list.
 
On Button Click Start The Audio Using C# Scripts In Unity
 
Click on the Play button. Select Button click Start on Audio Source.
 
On Button Click Start The Audio Using C# Scripts In Unity
 

Summary

 
I hope you understood how to play audio on a button click using C# scripts in Unity.