problem with my simple script please help.

here is my script

using UnityEngine;
using System.Collections;

public class countdowntimer : MonoBehaviour 
{
	public float timer;
	public float timerLimit = 30.0f;

	public GUIText timerText;
	// Use this for initialization
	void Start () 
	{
		timer = timerLimit;
		SetTimerText ();
	}
	
	// Update is called once per frame
	void Update () 
	{
		timer -= Time.deltaTime;
		SetTimerText ();

		if(timer <= 0.0f) 
		{
			print ("You Lose!");
			timer = timerLimit;

		}
	}

	void SetTimerText()
	{
		timerText.text = "Time Left: " + timer .ToString("f0");
	}
}

the problem is it wont print the message in this case YOU LOSE! i have the same problem with my win script aswell. everything else works it just wont print when the float is equal to or less then 0.0f. what is going on?

I can confirm that both scripts have no errors. I have tested them by themselves with only the necessary objects and they functioned as expected. So it must be something in the way the scene is setup. Or some ghost in the machine.

This is a list of my best guess issues. I know some are redundant. Sorry.

  • Are the scripts in the scene?
  • Is more than one object changing the
    gui text?
  • Are all of the public variables
    assigned correctly?
  • Are your tags set with correct
    spelling/capitolization?
  • Is the On trigger function on an
    object with a collider set as
    trigger?
  • Does one object that’s colliding have
    a rigidbody?
  • have you tried restarting Unity or moving everything to a new project?

Others may add to this list if you think of anything.

where you put -

 if(timer <= 0.0f)
{
      print ("You Lose!");
      timer = timerLimit;
}

Add in “loseFlag” as a public boolian (which is =false) and you could always swap it for -

public bool loseFlag = false;
.
.
.
 if(timer <= 0.0f)
{
     loseFlag = true;
     timer = timerLimit;
 
}

and add in the routine -

void OnGUI(){

   if(loseFlag){
      GUI.Label(Rect(0,0,Screen.width,Screen.height),"You Lose");
   }
}

hopefully that will work for you! :slight_smile: