- Home /
Playing a soundclip at random from an array. C#
I've never really worked with audio coding before but I need to play a sound clip at random from a group of 5 whenever the player hits an object. Right now I've just got it in some test code and once I've got things figured out I'll trasplant it into the correct code.
public AudioSource[] DamageClips;
public GameObject Thief;
void Start ()
{
Thief = GameObject.FindGameObjectWithTag ("Thief");
AudioClip nextClip = Random.Range (0, DamageClips.Length);
audio.clip = nextClip;
void TouchHasBegun()
{
Ray ray = Camera.main.ScreenPointToRay( screenPos );
RaycastHit hit;
if ( Physics.Raycast( ray, out hit ) )
{
hit.collider.gameObject.SendMessage("OnClick", SendMessageOptions.DontRequireReceiver);
//Debug.Log("PAUSE GODDAMN IT");
}
//if (falling == false && levelStart == true && jumpHasPlayed == false)
//{
//this.rigidbody.useGravity = true;
//levelStart = false;
//falling = true;
//Camera.main.backgroundColor = Color.blue;
//}
else if(camTransform.position.y <= 170.0f)
{
anchor.rigidbody.constraints = RigidbodyConstraints.FreezeAll;
Debug.Log("STOP");
Thief.animation.Stop("Falling");
Thief.animation.Play("Grabbing");
isFallingPlaying = false;
audio.Play(DamageClips[Random.Range (0,DamageClips.Length)]);
}
I've looked through some other answers, which is where I got what I have here but I'm getting a few errors.
Cannot implicitly convert type int' to
UnityEngine.AudioClip'
The best overloaded method match for UnityEngine.AudioSource.Play(ulong)' has some invalid arguments Argument
#1' cannot convert UnityEngine.AudioSource' expression to type
ulong'
To be quite honest, I have no idea what I'm doing here so I don't really know how to fix these. Any help would be greatly appreciated.
Also, I have taken chunks my code out to post what I have here, most of it has nothing to do with the audio.
Answer by getyour411 · Apr 10, 2014 at 12:49 AM
I couldn't help but envision Cartman when I read
//Debug.Log("PAUSE GODDAMN IT");
Take a look at this question/answer for a simple syntax example
http://answers.unity3d.com/questions/403748/array-of-audioclips-not-playing-please-help.html
Yeah, I was working on getting a pause button working and making Debugs relieves some stress.
I was able to get rid of the errors but now the soundclips won't play.
I've got an audiosource on the object (do I need to make an audio source for every clip?) and an audiolistener on the camera, my computers volume is set to max, the audiosource is within the $$anonymous$$imum range of the audiolistener and I've got the audiosource volume as high as it'll go. I've put the clips in the array and the rest of the touch code works so I know it's detecting it. Is there anything else I would need to do to make it work? I'm using the mobile unity remote app for touch control testing.
Answer by FeedMyKids1 · Jun 17, 2020 at 01:10 AM
You need to change DamageClips to be AudioClips instead of AudioSources: public AudioSource[] DamageClips; to public AudioClip[] DamageClips;
Then, in 'Start' change this AudioClip nextClip = Random.Range (0, DamageClips.Length); to AudioClip nextClip =DamageClips[ Random.Range (0, DamageClips.Length)];
Your answer
![](https://koobas.hobune.stream/wayback/20220613142351im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Can't get sound clip from array to play properly 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Using a footstep C# script and keep getting an "IndexOutOfRangeException" 2 Answers
Make an array of scripts? 1 Answer