Fade out and triggered load of next scene not working after update

I recently updated my storybook app and a function has stopped working. I had a script on the gameobject with my audio track that faded the screen to black and loaded the next scene when the audio track ended.

I’m an artist and had hacked this script together years ago with the help of some forum posters - could someone tell me how to change the script to make it work again? I assume some syntax has been made obsolete or something?

function Start()
{
     Screen.sleepTimeout = 0.0f;    // turn off auto-dimming
     PlayAudio();
}

function PlayAudio()
{
     GetComponent.<AudioSource>().Play();
     while(GetComponent.<AudioSource>().time < GetComponent.<AudioSource>().clip.length){
          yield; //if the audio is playing, don't do anything
     }
     //when script gets to here, the audio has stopped playing - load your level
    
    if(AdventManager.AdventMode)
    {
        // In Advent mode, fade scene halfway
        GetComponent.<Renderer>().enabled = true;
       
           Fade.use.Alpha(GetComponent.<Renderer>().material, 0.0, 0.0, 0.0);
                   
        Fade.use.Alpha(GetComponent.<Renderer>().material, 0.0, 0.5, 0.5);
       
        yield WaitForSeconds(0.5);
       
        // And display back/forward/rewind buttons
        // Instantiate(Resources.Load("EndScene"));
        var fwd : GameObject = GameObject.Find("BTN_Fwd");
        var back : GameObject = GameObject.Find("BTN_Back");
        var reload : GameObject = GameObject.Find("BTN_Reload");
        Fade.use.Alpha(fwd.GetComponent.<Renderer>().material, 0.0, 1.0, 0.0);
        Fade.use.Alpha(back.GetComponent.<Renderer>().material, 0.0, 1.0, 0.0);
        Fade.use.Alpha(reload.GetComponent.<Renderer>().material, 0.0, 1.0, 0.0);
    }
    else
    {
        // Fade to black
        GetComponent.<Renderer>().enabled = true;
       
        Fade.use.Alpha(GetComponent.<Renderer>().material, 0.0, 0.0, 0.0);
               
        Fade.use.Alpha(GetComponent.<Renderer>().material, 0.0, 1.0, 0.8);
       
        yield WaitForSeconds(0.8);
       
        // And goto next page
        Application.LoadLevel (Application.loadedLevel+1);
    }

}

Another detail - the fade out and level load work when I hit the “page turn” button in the app - it is the one triggered by the end of the audio that seems to exclusively be the issue.

It seems like this is never leaving the while loop:

while(GetComponent.<AudioSource>().time < GetComponent.<AudioSource>().clip.length){
          yield; //if the audio is playing, don't do anything
     }
Debug.Log("TotalLength:" +GetComponent.<AudioSource>().clip.length);
while(GetComponent.<AudioSource>().time < GetComponent.<AudioSource>().clip.length){
             Debug.Log("CurrentTime: " +GetComponent.<AudioSource>().time);    
              yield; //if the audio is playing, don't do anything
         }

Make sure your values are what you think they should be. I will say that I wish storing these getcomponent calls into a variable had been suggested/taught to you, but that’s another issue.

1 Like

Ha, thanks, I would say nothing was ever formally taught to me, but in case you are curious, the solution was to replace this:

while(GetComponent.<AudioSource>().time < GetComponent.<AudioSource>().clip.length){
              yield; //if the audio is playing, don't do anything
}

with:

yield WaitForSeconds(GetComponent.<AudioSource>().clip.length);