- Home /
Only one audio active. Others do not activate
I'm trying to make a voice dialogue. They have to speak in turn but only one audio active, others do not activate.How can i activate together all of them
void OnTriggerStay(Collider col)
{
if (col.gameObject.name == "fps" && gor==1)
{
audio.clip = b1;
audio.Play();
Text1.SetActive(true);
if (Input.GetKeyDown(KeyCode.F))
{
gor = 2;
Text1.SetActive(false);
if (sira == 0)
{
audio.clip = b2;
audio.Play();
sira = 1;
if (!audio.isPlaying)
{
audio.clip = b3;
audio.Play();
sira = 2;
if (!audio.isPlaying)
{
audio.clip = b4;
audio.Play();
sira = 3;
if (!audio.isPlaying)
{
audio.clip = b5;
audio.Play();
sira = 4;
if (!audio.isPlaying)
{
audio.clip = b6;
audio.Play();
sira = 5;
if (!audio.isPlaying)
{
audio.clip = b7;
audio.Play();
}
}
}
}
}
}
}
}
Answer by MUG806 · Feb 19, 2021 at 07:15 PM
Is the idea to play each clip in sequence when the last one finishes? You have the right sort of idea, your if-statements are just set up wrong. You want something more like this:
if (!audio.isPlaying) {
if(audio.clip == b1) {
audio.clip = b2;
audio.Play();
}
else if(audio.clip == b2) {
audio.clip = b3;
audio.Play();
} //.... etc ...
}
The above code should work fine, just takes a while to write a bit for every single audio clip. If you want a better solution you can try using an array and a for loop. If you need an example of that let me know and I can write something up for you
Answer by emirayranci18 · Feb 19, 2021 at 11:23 PM
@MUG806 Thanks for your help but I don't understand how to use the array and for loop Can you give an example?
I was actually mistaken and you dont even need a for loop:
public AudioClip[] clips //Drag all your sound clips into this in the inspector.
int nextClip= 0;
void Update(){
if (!audio.isPlaying){
if(nextClip < clips.length){
audio.clip = clips[nextClip];
audio.Play();
nextClip++;
}
}
}
This works by storing the clips in an array, and storing a number for which clip we need to play next. Each time the audioSource isn't playing, we check if we got to the end of all the clips, and if not, we play the clip at the "nextClip" number we stored, and then add 1 to the stored number so that next time we play the next clip.
Your answer
Follow this Question
Related Questions
Problem with playing sound on Trigger-Enter 2 Answers
How to get decibel's value of game's sound? 0 Answers
Glitched Music 0 Answers
Play audio at consistent volume 2 Answers
AudioSource won't play 2 Answers