I am trying a trigger an AudioClip to play when my Player bites the dust. So I trigger the UnityEvent, “onDie.Invoke()”. This will trigger a method which simply plays my audio clip.
But since it is an asynchronous call, the library I am using, fades the screen to black before I can hear the entire clip. Is there a way I can make the script ‘wait’ for the Invoke or AudioClip to finish?
How do you put the screen in black? because if I understand what you say is that the scene ends before the sound finishes sounding what you can do is that the sound system put it above everything in the hierarchy or make it last the clip less or that lasts longer the scene
A UnityEvent is a synchronous call. However when you start playing an audio that’s an asyncronous call. As Poseidon said it all boils down to how you “fade your screen” to black.
When you use a coroutine you can simply wait for the length of the audio clip before you start your fading. Though another way is to use Unity’s “Invoke” method. It allows you to call another method on a MonoBehaviour after a specified time.
So if “OnEvent” is the method that is invoked through your UnityEvent this will start playing the audio and basically “queue” the calling of StartFading after 5 sec. Instead of using the magic number “5” you can use the “Length” of the AudioClip that you want to play, so StartFading starts exactly when the clip has finished playing.