- Home /
Wait until audio is finished before set active is false
Hi,
in my project I want a sound to play and then for an objects active state to be set to false, at the moment both are happening at the same time so the sound doesn't play. If I keep the active state as true then I will hear the sound play.
How can I make sure that the audio finishes before the set active state is switched to false, any help or advice is appreciated.
Here's a selection of my code;
void OnTriggerEnter(Collider other)
{
if (other.tag == "Pick Up")
{
if (!isPlayed) {
source.Play ();
isPlayed = true;
}
}
if (other.gameObject.CompareTag ("Pick Up"))
{
other.gameObject.SetActive (true);
count = count + 1;
SetCountText ();
}
}
Answer by Ali-hatem · Mar 23, 2016 at 11:03 AM
void OnTriggerEnter(Collider other){
if (other.tag == "Pick Up") {
StartCoroutine (example());
}
}
IEnumerator example(){
source.Play ();
yield return new WaitWhile (()=> source.isPlaying);
//do something
}
Answer by tanoshimi · Mar 23, 2016 at 10:57 AM
Destroy takes an optional parameter stating the amount of time to pause before destroying an object - it's pretty clear in the documentation.
So you just pass the length of the audio clip as the delay:
Destroy (gameObject, source.clip.length);
Your answer
Follow this Question
Related Questions
Get an object to play a sound if player rolls over a trigger. 2 Answers
Need help making an audio trigger. 2 Answers
Making a sound play only once with a Boolean variable in javascript 1 Answer
Checking collision between 2 triggers based on distance. Possible? 1 Answer
Activate sound without Pro filters 0 Answers