- Home /
Play a random audio clip when button is clicked
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();
}
}
Thank you in advance.
Do you have an active audio listener near the PlaySound object if you have it configured that way? Add a debug.log into LetsPlay to verify it is being executed.
Okay so I took your advice and It stopped at GetComponent().clip = clips[clipPick];
$$anonymous$$issingComponentException: There is no 'AudioSource' attached to the "Soundmanager" game object, but a script is trying to access it. You probably need to add a AudioSource to the game object "Soundmanager". Or your script needs to check if the component is attached before using it.
That's the error I get. In the soundmanager, which is an empty game object, I have all of the sounds put into a list. Every time I add an audio listener to the Soundmanager, it tells me there can only be one audio listener.
Answer by Hydroid · Dec 15, 2016 at 04:21 AM
Okay so I fixed it. Apparently, I wasn't supposed to put anything into the AudioSource once i put it into the Soundmanager.
Answer by salex100m · Dec 14, 2016 at 02:13 AM
Does this work? I guess you can do the same thing with clips, but i would use AudioSource
void Awake () {
AudioSource[] audios = gameObject.GetComponents<AudioSource>();
}
public void LetsPlay(){
int clipPick = Random.Range(0, audios .length);
audios [clipPick].play();
}
Thank you! I had to fix some errors, of course. But Here is what I got.
AudioSource[] audios = gameObject.GetComponents<AudioSource>();
public void LetsPlay()
{
int clipPick = Random.Range(0, audios.Length);
audios[clipPick].Play();
}
The only error I get is "A field initializer cannot reference the non-static field, method, or property 'Component.gameObject'
Your method is most likely static. $$anonymous$$ake it non-static by removing the static keyword.
I checked the script; there is nothing static. Any other ideas? And thank you for trying to help me so far
Answer by jkaniuk · Dec 15, 2016 at 03:00 PM
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]);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612102615im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
how to stop an audio source from playing multiple times; change button text 0 Answers
Does enabling and disabling an AudioSource cause the sound to play, if playOnAwake is enabled? 1 Answer
Playing an audio clip 4 Answers
Using yield WaitForSeconds after AudioSource.PlayClipAtPoint 1 Answer
A better script for a machine gun? 1 Answer