Playing and Pausing multiple audio clips
Hello! I am pretty new at unity and c# and struggling on how to use two (or more) separate audio clips on my GameObject.
I have a sort of "footsteps on grass" sound that is playing when:
" float orizzontale = Input.GetAxis("Horizontal");" is > or < than 0 and stop when it's 0.
Now footsteps in the grass is working fine: It stops and it comes again ... but when I jump I can't hear sound.
I think the reason is that i put AUDIOSOURCE in pause or stop. How can I pause just a clip and not the whole Audiosource?
void SuoniPorco(float orizzontale) {
if (orizzontale < 0 && !suoni.isPlaying && !porcoInVolo || orizzontale > 0 && !suoni.isPlaying && !porcoInVolo) **//If my player is moving and is not jumping, then ...**
{
suoni.PlayOneShot(cammina); **// Play Sound Clip Until ....**
}
if (Mathf.Abs(orizzontale) < 0.001 || porcoInVolo) **//Player is not moving or he's jumping**
{
suoni.pause(); **//If so ... PAUSE this clip**
}
**// IF player Jumps ... then PLAY a JUMP sound**
if (porcoSalta) {
suoni.PlayOneShot(salto);
porcoSalta = false;
}
}
Answer by Feretro · May 07, 2018 at 09:00 PM
I've found a solution, but I don't know IF this is the right way to do:
I dragged my TWO (by now!) sounds into the Hierarchy and then, in the code I created a:
public AudioSource[] sounds;
I DID NOT assigned that "sounds" variable to the AudioSource Component (why? I had an error!) ... I did not code this line:
sounds = GetComponent<AudioSource>(); // I do Not have this line in my code
Then, back in the UnityEditor, I set the array size to 2 and then dragged two AudioClips from hierarchy.
and so, I can access to sounds separately:
sounds[0].Play();
sounds[1].Play();
or
sounds[0].Pause();
sounds[1].Pause();
I don't know if this is the right way to ... probably not ... but now my two sounds work together well! :-)
Your answer
![](https://koobas.hobune.stream/wayback/20220612162217im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to Send Audio Clip Via Facebook Messenger ? 0 Answers
Playing many AudioSources 1 Answer
Second AudioClip won't play 0 Answers
My sound effect is not playing 0 Answers