I could not find any forum post related to my problem. I have a script that has no errors, yet, when I click the button to play a random sound, nothing happens. I have the script hooked up to an empty game object and I have the script and audio sources all hooked up there. I am using the right function in the OnClick() for the button but I’m confused on why nothing happens.
public class PlaySound : MonoBehaviour {
public AudioClip[] clips;
public void LetsPlay()
{
int clipPick = Random.Range(0, clips.Length);
GetComponent<AudioSource>().clip = clips[clipPick];
GetComponent<AudioSource>().Play();
}
}
Hi @Hydroid, I see you have solved you problem, however I went ahead and wrote a quick script which you can compare to. I’m just wondering why you have an array of AudioSources rather than an array of AudioClips? Hope this helps
public class Player1 : MonoBehaviour
{
private AudioSource a_source;
public AudioClip[] a_clips;
void Start ()
{
a_source = gameObject.AddComponent<AudioSource>();
//can also switch add to get if object already has an audio source
}
void Update ()
{
if(Input.GetMouseButtonDown(0))
PlayRandomSound();
}
public void PlayRandomSound()
{
int selection = Random.Range(0, a_clips.Length);
a_source.PlayOneShot(a_clips[selection]);
}
}