- Home /
wait for audio to be finished than advance
Hi guys, when I raycast a object a audio will be played. right now, it will be played but when I raycast another object, it will play the audio of the other object instead finishing the first audio. I want it to wait for the audio is finished before playing audio 2 (like in waiting for eachother). see my code.
function Update () {
if (Physics.Raycast (transform.position, transform.forward, hit, 100) && (hit.collider.gameObject.tag == "kat"))
{
hitter = true;
Print("I am looking at" + hit.transform.tag);
{
if(audioKathasplayed == false)
{
audioKathasplayed = true;
audio.clip = audioKat;
audio.Play();
}
Timer += Time.deltaTime;
if (Timer > 20)
Destroy(hit.collider.gameObject);
}
}
else if (Physics.Raycast (transform.position, transform.forward, hit, 100)&&(hit.collider.gameObject.tag == "boek"))
{
hitter = true;
Print("I am looking at" + hit.transform.tag);
{
if(audioBoekhasplayed == false)
{
audioBoekhasplayed = true;
audio.clip = audioBoek;
audio.Play();
}
Timer += Time.deltaTime;
if (Timer > 27)
Destroy(hit.collider.gameObject);
}
}
else
{
hitter = false;
{
Timer = 0.0;
Print("I am looking at nothing");
}
}
}
function DestroyObject(){
if (Physics.Raycast(transform.position, transform.forward, hit))
{
if(hit.collider.gameObject.tag == "pen")
{
yield WaitForSeconds(audio.clip.length);
Destroy(hit.collider.gameObject);
}
}
}
Not sure you asked a question. What's your problem?
Answer by Junske · Dec 18, 2014 at 01:21 PM
ok I managed to make it work. only thing is when the object get destroy i get "NullReferenceException: Object reference not set to an instance of an object" what am I doing wrong?
EDIT: this is the right way to write it. you guys can use it! thanks to MrSoad
function Update () {
if (Physics.Raycast (transform.position, transform.forward, hit, 100) && (hit.collider.gameObject.tag == "kat"))
{
hitter = true;
Print("I am looking at" + hit.transform.tag);
if(audio.isPlaying == false)
{
StartCoroutine(PlayAndDestroy(hit.collider.gameObject));
}
}
else if (Physics.Raycast (transform.position, transform.forward, hit, 100)&&(hit.collider.gameObject.tag == "boek"))
{
hitter = true;
Print("I am looking at" + hit.transform.tag);
if(audio.isPlaying == false)
{
StartCoroutine(PlayAndDestroy(hit.collider.gameObject));
}
}
else if (Physics.Raycast (transform.position, transform.forward, hit, 100) && (hit.collider.gameObject.tag == "pen"))
{
hitter = true;
Print("I am looking at" + hit.transform.tag);
if(audio.isPlaying == false)
{
StartCoroutine(PlayAndDestroy(hit.collider.gameObject));
}
}
else
{
hitter = false;
Timer = 0.0;
Print("I am looking at nothing");
}
}
function PlayAndDestroy(oHit_Object : GameObject){
if(oHit_Object.tag == "kat")
{
audio.clip = audioKat;
audio.Play();
yield WaitForSeconds(audioKat.length);
Destroy(oHit_Object);
}
else if(oHit_Object.tag == "boek")
{
audio.clip = audioBoek;
audio.Play();
yield WaitForSeconds(audioBoek.length);
Destroy(oHit_Object);
}
else if(oHit_Object.tag == "pen")
{
audio.clip = audioPen;
audio.Play();
yield WaitForSeconds(audioPen.length);
Destroy(oHit_Object);
}
}
PlayAndDestroy() is a coroutine, this does not run in Update, it runs along side it, which is right. The problem is that your raycast is in Update and this may have run again, changing your raycast "hit" data. When you start this coroutine you should pass the current hit data to it so it has a copy of the valid data to use when it runs.
could you help me with this? i'm pretty stuck with this one..
Try this, when you start your coroutines use :
StartCoroutine(PlayAndDestroy(hit.collider.gameObject));
and alter your PlayAndDestroy function like this :
function PlayAndDestroy(oHit_Object : GameObject){
if(oHit_Object.tag == "kat")
{
audio.clip = audio$$anonymous$$at;
audio.Play();
yield WaitForSeconds(audio$$anonymous$$at.length);
Destroy(oHit_Object);
}
else if(oHit_Object.tag == "boek")
{
audio.clip = audioBoek;
audio.Play();
yield WaitForSeconds(audioBoek.length);
Destroy(oHit_Object);
}
else if(oHit_Object.tag == "pen")
{
audio.clip = audioPen;
audio.Play();
yield WaitForSeconds(audioPen.length);
Destroy(oHit_Object);
}
}
Answer by HYPERSAVV · Dec 18, 2014 at 01:21 AM
You can check to see if a clip is playing using audio.isPlaying(). You are using the same variable (audio) for both Kat and Boek, so this should be doable.
http://docs.unity3d.com/ScriptReference/AudioSource-isPlaying.html
However, it still looks like there are issues regarding how you dispose of these objects. Consider something like this:
if(Physics.Raycast (transform.position, transform.forward, hit, 100)&&(hit.collider.gameObject.tag == "boek"))
{
// We don't really care if Kat is playing, we care if anything is currently playing
if(audio.isPlaying() == false)
{
StartCoroutine(PlayAndDestroy(audioBoek,hit.collider.gameObject));
}
}
IEnumerator PlayAndDestroy(AudioClip clip, GameObject someObject)
{
audio = newClip;
audio.Play();
yield WaitForSeconds(audio.clip.length);
Destroy(someObject);
}