Gui time bar running before scene loaded

Hello,

Ive got a game, each screen is in a separate scene. etc.

I have an issue with my level GUI. Ive got a time bar decreasing in size using a texture. Odd thing is that this script runs before this scene is loaded. Ive also noticed this particular script or correctly put the instantiation of this script doesnt reset like others when leaving and reloading the scene.

The script is below, could anyone tell me why this script is behaving like this? Thank you :slight_smile:

var timeProgress : float;    
var pos : Vector2;    
var size : Vector2;    
var pos2 : Vector2;    
var size2 : Vector2;    
var pos3 : Vector2;    
var size3 : Vector2;    
var timeBarEmpty : Texture2D;    
var timeBarFull : Texture2D;    
var timeBarCover : Texture2D;    
var timeIcon : Texture2D;    
var speed : float;    
var d1 : float;    
var barLength : float;

function OnGUI()
{
    GUI.depth = d1;
    GUI.DrawTexture(Rect(pos.x, pos.y, size.x, size.y), timeBarEmpty);
    GUI.depth = d1;
    GUI.DrawTexture(Rect(pos.x, pos.y, barLength, size.y), timeBarFull);
    GUI.depth = d1;
    GUI.DrawTexture(Rect(pos2.x, pos2.y, size2.x, size2.y), timeBarCover);
    GUI.depth = d1;
    GUI.DrawTexture(Rect(pos3.x, pos3.y, size3.x, size3.y), timeIcon);
}

function Start()
{
	ResetGUI();
}

function Update()
{
    barLength = size.x * timeProgress;

    if(timeProgress > 0)
    {
        timeProgress = 1-(Time.time * speed);
    }
    else
    {
    	EndGame();
    }
}

function EndGame()
{	
	yield WaitForSeconds(2);
	Camera.main.SendMessage("fadeOut");
	yield WaitForSeconds(1.5);
	Application.LoadLevel("MainMenu");
}

function ResetGUI()
{
	timeProgress = 1;
	size.x = 310;
	barLength = 310;
}

Looks to me like every frame where timeProgress > 0, EndGame() will be called… Because you’re running it as a co-routine (it has a yield in it)…

Ok bug solved, thanks for the input guys, with the coroutine thing im not super knowledgable about that area but I was having issues with it before this bug and I got around it by putting the yield statement in another function.

Anyway onto the fixing of the bug. Line reading:

timeProgress = 1-(Time.time * speed);

this line was either;

  • Assigning a timeProgress value with time taken from its first initialisation, good for the first go and rubbish for any go after that
  • Or most likely something todo with my stupid maths and the “1-”

Anyway I know its not the most ideal situation but I changed the line to this:

timeProgress -= speed;

Simple, just have to hope my frame rate doesnt change too much.