- Home /
Wait for clip finish and activate other audio source
Hi, I've a little question.
I want to play a sound and desactivate ALL others audio sources if the player pick some stuffs. In my script bellow, the sound stop and the new play very well. But I'm unable to reactivate all the others audio sources AFTER the clip has finished...
Here my C# script :
public class Sign : MonoBehaviour {
private bool revertFogState = false;
public Font f;
public int SignsFound = 0;
public AudioClip pickSound;
public AudioClip DANCE;
public Transform explosion;
public Transform win;
private GameObject toDestroy;
private bool destroyObjectSwitch;
private bool pickup;
private bool collided;
private int tempo;
void Start () {
this.destroyObjectSwitch = false;
this.pickup = false;
this.collided = false;
}
void Update () {
if(Input.GetButton("Fire1")){
this.pickup = true;
}else{
this.pickup = false;
}
Debug.Log ("pickup : "+this.pickup+" Collided : "+this.collided);
if (Input.GetButtonDown("Pickup") && this.collided == true){
AudioSource.PlayClipAtPoint(pickSound, transform.position );
Instantiate(explosion, transform.position, transform.rotation);
Destroy(this.toDestroy.gameObject);
this.SignsFound++;
this.pickup = false;
this.collided = false;
this.tempo = 100;
if (SignsFound == 1){
AudioSource[] audios = FindObjectsOfType(typeof(AudioSource)) as AudioSource[];
foreach(AudioSource aud in audios)
aud.volume = 0;
AudioSource.PlayClipAtPoint(DANCE, transform.position );
Instantiate(win);
yield return new WaitForSeconds(audio.clip.length);
AudioSource[] audios = FindObjectsOfType(typeof(AudioSource)) as AudioSource[];
foreach(AudioSource aud in audios)
aud.volume = 0;
RenderSettings.fog = revertFogState;
}
}
if(this.tempo>0)this.tempo--;
}
void OnTriggerEnter(Collider other){
Debug.Log (other.gameObject.name);
if(other.gameObject.tag == "Sign" ){
this.collided = true;
this.toDestroy = other.gameObject;
}
}
void OnTriggerExit(Collider other){
this.collided = false;
this.toDestroy = null;
}
void OnGUI(){
if(this.tempo>0)
GUI.Label (new Rect (Screen.width/2-100,Screen.height/2-10,500,500), "Tu as "+this.SignsFound+" panneau (x) SUR 5.");
GUI.skin.font = f;
}
}
I tried to make a Yield returne new for the clip, but I cant reactivate the others sounds. Can you help me ? ^^
Answer by fafase · Jun 29, 2014 at 05:41 PM
Your problem is that you are trying to yield from an update method and that is not possible.
You need to create a coroutine to start.
if (SignsFound == 1){
StartCoroutine(PlaySound());
}
IEnumerator PlaySound(){
AudioSource[] audios = FindObjectsOfType(typeof(AudioSource)) as AudioSource[];
foreach(AudioSource aud in audios)
aud.volume = 0;
AudioSource.PlayClipAtPoint(DANCE, transform.position );
Instantiate(win);
yield return new WaitForSeconds(audio.clip.length);
// No need for this line below you already have the array
// AudioSource[] audios = FindObjectsOfType(typeof(AudioSource)) as AudioSource[];
foreach(AudioSource aud in audios)
aud.volume = 1; // Here I think you meant 1 to set them back to normal
RenderSettings.fog = revertFogState;
}
Thanks ! But I've got an error !
Unexpected symbol : line 52
Can you post the entire script to check if I'm doing something wrong please ? ^^
The first part goes in the Update, the second part goes in the class as a method.
Sorry, I don't understand :/ EDIT : I figured out ! Thanks ! ;)
Your answer
![](https://koobas.hobune.stream/wayback/20220613152813im_/https://answers.unity.com/themes/thub/images/avi.jpg)