Hello, I am new to Scripting and I ran into a problem.
Here is an example of what I am trying to do:
var clip : AudioClip;
function OnGUI {
if (GUI.Button (Rect (0,0,0,0), "Level 1")) {
audio.PlayOneShot(clip);
// Time Delay
Application.LoadLevel(1);
}
}
Where I put // Time Delay, I needed something to delay Application.LoadLevel(1); from happening yet, so I could hear the full audio clip before loading level 1.
Hummm, if I were you, I wouldn’t use PlayOneShot. Make sure you have an AudioSource Component on your object, set it to playOnAwake = false, loop = false, then the following code will wait the precise time of your clip before launching the next level.
var myClip : AudioClip;
function OnGUI() {
if (GUI.Button (Rect (0,0,100,35), "Level 1")) {
audio.clip = myClip;
audio.Play();
WaitTillClipFinishesAndLoadLevel();
}
}
function WaitTillClipFinishesAndLoadLevel(){
while(audio.isPlaying)
{
yield;
}
Application.LoadLevel(1);
}
P.S.: Audio is my specialty… In case you need help!
As correctly stated by @OrangeLightning you cannot yield a OnGui. So you need a coroutine.
var clip : AudioClip;
var second : float;
function OnGUI (){
if (GUI.Button (Rect (0,0,0,0), "Level 1")) {
audio.PlayOneShot(clip);
Wait();
}
}
function Wait(){
yield WaitForSeconds(second);
Application.LoadLevel(1);
}