Sound Effect Array plays all sounds when instructed to play one
I created an array to hold all of my character's voice effects, activate a script with an onTriggerEnter event, instruct the array to play a sound based off a random int, and it plays all of them at once. I don't know why. Please help me because this game has to get out quickly. Thanks in advance!
private void OnTriggerEnter(Collider collision)
{
Player.AddForce(boostIntensity * Vector3.forward.normalized, ForceMode.VelocityChange);
boostParticlesL.Play();
boostParticlesR.Play();
int sfxIndex = Random.Range(0, characterBoostSFX.Length);
characterBoostSFX[sfxIndex].Play();
}
Answer by DRAmos97 · Jan 03, 2018 at 09:45 AM
Yep, the same thing is happening to me and it's driving me crazy. I'm thinking creating GameObjects with the sound effects slapped on to them and instantiate them through a GameObject Array instead. Might share the code, if it works.
(Edit) Turns out my problem was with the audio files themselves. Not sure it this will help you, but this is my code. :P I think you are playing the AudioIndex rather than AudioSource itself.
void PlaySound()
{
myAudioSource.pitch = Random.Range(pitchMin, pitchMax);
if (myAudioClip != null)
{
randomClipNumber = Random.Range(0, myAudioClip.Length);
myAudioSource.clip = myAudioClip[randomClipNumber];
myAudioSource.Play();
}
else
{
myAudioSource.Play();
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612140902im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Checking if a point is withing any of the colliders 1 Answer
Array 2D of data 1 Answer
Creating a Grid using an Array with C# 1 Answer
Storing delegates or function calls in a array 0 Answers
Index out of range error 1 Answer