- Home /
Advance when the sound is done playing
Hello everyone. I have encountered a problem that I have not been able to figure out after a few days of tweeking things around. Right now I have a scene where you are taken from point to point, like a rail shooter. At each point you click on objects to learn about them, and some of them even have sound. After you click on each object a question pops up. My problem is that the question pops up right when you click on an object, even if that object has a sound attached to it. If the object has a sound attached to it I would like the sound to finish before the question pops up. The sound is attached to one object and the script to pop up the question is attached to another. Is there a way to determine if the sound on another object is finished playing? I will attach a bit of my script for the clicking on objects and the question popping up:
if(hit.collider.gameObject.name == "oilDriBag")
{
GameObject oildribag = GameObject.Find("oilDriBag");
oildribag.GetComponent<AudioSource>();
if(audio.isPlaying)
{
audioIsPlaying = true;
}
if(!audioIsPlaying)
{
object1Clicked = true;
}
}
That part is in the update function. The onGUI function has the code for popping up the question. It simply just draws boxes and buttons, but I don't want that to happen until the sound has finished playing from the objects clicked in the code above.
EDIT: I have put in the code that I have now, but I am obviously doing something wrong.
Answer by DaveA · May 13, 2011 at 02:50 PM
Two things come to mind 1. There is an 'isPlaying' member on the audio class, which you can monitor. Let it go true first to make sure it's playing, then watch for it going false. 2. You can find out the length of the clip and use that as a timer (don't allow click until that amount of time has elapsed).
http://unity3d.com/support/documentation/ScriptReference/AudioSource-isPlaying.html
How would I check if audio isPlaying on another gameObject from a script other than the one that controls my camera? I tried checking for it before but since it is attached to another game object using another script I don't know how to check for it.
http://unity3d.com/support/documentation/ScriptReference/index.html and click on Accessing Other Components (the rest of those links are good reading too!)
I'm still not grasping the concept. I'll get there eventually, but not there yet. I have updated my code to show you what I have, but it still isn't working right. I get an error saying "There is no AudioSource on main camera but a script is trying to access it" I want to get the AudioSource from the oilDriBag game object though, not the main camera. Any idea what my mistake is?
You get the audio source but you don't do anything with it. Assign it to a var, then check it's isPlaying: var aud = oildribag.GetComponent(); if (aud.isPlaying) etc etc
Also, you don't need to do a Find, you already have the game object on the 'hit': hit.collider.gameObject.GetComponent()
Your answer
Follow this Question
Related Questions
GUI Box not showing up after object clicked 1 Answer
raycast with transparent shader 1 Answer
Saving selections across multiple menus? 1 Answer
Vertical distance indication 1 Answer
How do disable uGUI/EventSystem selection feature? 0 Answers