Execution of Scenes

Hello All,

I am new to Unity and I am in the middle of a game project for the US Army. Here is my question.

I have 2 scenes set up. In my second scene, I have a script that handles my GUI controls. There is a small countdown feature in the OnGUI() function; sort of a ready, set, go using textures.

So here is what happens. I start the game from my first scene. The first scene just has a “Play Game” button that loads the second scene. So I sort of start at the button for a few seconds and then click it. My second scene loads, but the OnGUI has already fired and I don’t see my little countdown. It is as if the OnGUI function fired off before the second scene as even launched.

If I run Scene 2 on its own, all is right with the world. There only seems to be a problem when I run the game from Scene 1. My build order is correct. Is this normal or am I missing something?

And advice would be awesome.

Thank you!

I guess the problem is the way you’ve implemented your countdown. OnGUI is called every frame (at least once, see Event-class for more details). If you use Time.time directly it won’t work since the time keeps on running, even in your first scene.

You could use a coroutine to drive the texture switching, or just set a start-time so you start counting when the second scene has been loaded.

// C#
var startTime : float;

function Start()
{
    startTime = Time.time;
}

function OnGUI()
{
    var timePassedSinceStarted = Time.time - startTime;
    // use the "timePassedSinceStarted" value to do your count down.
    // this value will start counting at 0 since the offset from game start is removed
}