I Want It So That When I Tap On The Button On An Android Phone Or Tablet It Plays An Audio File
you don’t need any code for that if you don’t have to change clip @ runtime but you have to know how to use On Click Event if not see this Make buttons do something
then attach an AudioSource to the button & add audio clip the AudioSource then create On Click event then drag & drop the button to the On Click empty field then open the drop down menu next to it highlight the AudioSource then other panel will open select Play();
- You can add an AudioSource and attach it to any GameObject.
- Uncheck PlayOnAwake.
- Uncheck Loop as well if you don’t want it to play in a loop.
- Get reference of that GameObject in your script.
- Play it when needed.
Script:
using UnityEngine;
using System.Collections;
public class SoundManager : MonoBehaviour {
public AudioSource sound;
public AudioClip clipSound;
public bool playSound;
void Start()
{
sound.clip = clipSound;
playSound= false;
}
void Update ()
{
if (playSound)
{
sound.Play();
playSound= false;
}
}
}
You can update the value of the bool whenever you want to play sound, i.e. on your click on button.