3D Platform Tutorial (audio.isPlaying) Problem

Hi! I’m currently going through the 3D platform tutorial, and I’ve hit a snag. Hopefully someone can shed some light on the issue.

The “Game Over” screen is set up as specified in the tutorial with the following script:

function Update ()
{
  if (!audio.isPlaying || Input.anyKeyDown) {
  	Application.LoadLevel("StartMenu");
  }
}

Instead of staying on the Game Over screen, it flashes up for a couple of frames, and immediately continues to the Start Menu.

I debugged the code to the point where I have determined that the problem has something to do with the audio.isPlaying property. For some reason, the script doesn’t think that the audio is playing. If I remove that part out of the conditional statement, I can confirm that the audio does in fact play.

Is it possible that this code is executing before the scene gets a chance to start playing the audio? Would it be more reliable to start the audio playback via code instead of the inspector?

Thanks in advance!

I’m replying to my own post, so if someone else has this issue while going through the tutorials, they’ll have a solution for it.

When Unity plays the Game Over scene, the sound does not immediately begin playing. As a result of this, when the Update event is fired for the first time, audio.isPlaying returns false, causing it to skip the music and go straight back to the Start Menu scene.

Substituting the code below should make the menu work as expected:

private var initialized = false;

function OnPostRender()
{
  initialized = true;	
}


function Update ()
{
  if (initialized == true) {
    if (!audio.isPlaying || Input.anyKeyDown) {
      Application.LoadLevel("StartMenu");
    }
  }
}

I don’t know if this is a bug in the player, or simply a change in how Unity’s event system works. Either the tutorial should be updated so that the menu works as expected, or a player patch is in order. I utilized Unity’s “Report a Problem” feature so that there is a record of the issue.

Thanks for that. I just ran into the same problem and gathered that something like that was happening but didn’t know how to code a work around (first time scripting)

:slight_smile: