Playing Music After Seconds Works, But When Scene Reloads, Script Doesn't Work

Hello!

I have a probably really easy question but I have no Idea what do to. In my scene, I don’t have the music start or a GUI text disappear in till three seconds after the scene starts. So when I start my scene, everything works, but if I die, then I go back to the Main Menu, where I can select the same scene and try it again, but for some reason, the music starts right away, even though the GUI text still stays for three seconds then disappears. Here is the music script, don’t know if it will be much help.

var song : AudioSource;
 
function Update () {
     
    if (!song.isPlaying && Time.time >= 3) {
        song.Play();
    }
}

Any Thoughts?

Time.time gives you the time since the projects is started. So when you reload your scene, Time.time is always greater than 3 which causes the condition to be true and it plays the music.

  • You need to have another variable to store your time since the scene is started and always initialize it to 0 in Start() method.

  • Or you can use Invoke() to play song after 3 seconds like this.

       void Start()
     {
         Invoke("PlayMusic" , 3);
     }
     
     void PlayMusic()
     {
         song.Play();
     }