- Home /
How to set text(timer) to a fixed place
Hey there :) I am working on a project where I include a timer, but it seems that it won't stay at the same place, dependin on the game's resolution. I would like to stay on a specific place on the screen, how is it possible? Thanks for you advices.
Here is the timer's script, along with how I determine its position:
var startTimer: int;
var minutes : int;
var seconds : int;
var style : GUIStyle;
function Start() {
startTimer = Time.time;
}
function Update() {
var time = Time.time - startTimer;
minutes = time / 60;
seconds = time % 60;
}
function OnGUI() {
GUI.Label (Rect (850, 800, 200, 20), String.Format ("{0:0}:{1:00}", minutes, seconds), style);
}
Answer by devluz · Nov 05, 2014 at 10:52 AM
You will need to calculate the position relative to the resolution. Screen.width and Screen.height gives you the size of your screen. eg if you want to place it at the centre top side of the screen you could try Screen.Width / 2. If you want it on the bottom right corner try to put the start point 200 pixels from the right side:
e.g.
function OnGUI()
{
GUI.Label (Rect (Screen.width - 200, Screen.height - 20, 200, 20), String.Format ("{0:0}:{1:00}", minutes, seconds), style);
}
(example is untested. I am on a computer without unity right now)
Play a bit around with the values in Rect. I am sure you will find a good position.
Your answer
Follow this Question
Related Questions
How to clear a GUI Label 1 Answer
Can't draw GUI.Label text on subpixel values 0 Answers
GUI label/text appear/disappear 1 Answer
OnGUI called after LateUpdate screwing up debug text database 1 Answer
GUI & GUI Text Disappear When Publishing 6 Answers