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.
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.
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);
}
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