I need help repositioning coded UI

I need help repositioning a timer that I coded to the upper left hand corner of the screen. This is the code I have as of right now

#pragma strict
var timeRemaining : float = 60;

function Update () {
timeRemaining -= Time.deltaTime;
}

function OnGUI(){
if(timeRemaining > 0){
GUI.Label(new Rect(100, 100, 200, 100), "Time Remaining : "+timeRemaining);
}
else{
Application.LoadLevel(“Tutorial_Level_1”);
}
}

I see this is your first message. Welcome to the community!

There are a few things to note that will get you a leg up right away:

  • Always surround your code with CODE tags. The easiest way to do that is to use the “Insert” button, in the formatting toolbar right between the movie filmstrip and the floppy disk icon. Never paste code without it!
  • You’re using the legacy GUI system. There’s a dedicated forum for that.
  • But you almost certainly shouldn’t be using the legacy GUI system for this anyway.

The right way to do this is:

  • Add a Canvas to your scene

  • Add a Text, pinned to the top-right corner, with an initial text of “60”

  • Add another Text next to it that says “Time Remaining:”

  • In your script above, add a public UI.Text property named timeLeftText

  • Drag your numeric text (the one with a value of “60”) into this slot in your script component (on whatever object you’ve attached that script to)

  • Delete OnGUI, and change your Update method to:

function Update() {
    timeRemaning -= Time.deltaTime;
    if (timeRemaining < 0) {
        timeRemaining = 0;
        Application.LoadLevel("Tutorial_Level_1");  // TODO: make this a property too!
    }
    timeLeftText.text = timeRemaining.ToString();
}

That should do it!

1 Like

Thank you so much! This helped a lot.

1 Like