- Home /
How do I change the position of GUI text?
Hello, I'm a new to programming and I was wondering how I should modify my script to put my GUI text at top center of my screen. This is my script.public class HUDscript : MonoBehaviour {
float playerScore = 0;
// Use this for initialization
void Update () {
playerScore += Time.deltaTime;
}
void OnGUI ()
{
GUI.Label (new Rect (10, 10, 100, 30), "score: " + (int)(playerScore * 1));
}
}
Answer by Gruffy · Mar 27, 2014 at 10:04 PM
If you replace you entire GUI.Label line with the one below you should see this turn up in the middle and at the top.
GUI.Label (new Rect (Screen.width - (Screen.width * 0.5f)-50.0f, Screen.height - Screen.height, 100.0f, 30.0f), "score: " + (int)(playerScore * 1));
Now, if you look at the code, all that has been done is a call to the globally available screen parameters natively populated by Unity based on your screen size settings or the default ones if left alone.
what it is saying is... the new rectangle x position should be half of the screen width
minusscreen.width (the full width of screen)
thehalf of the screen width (Screen.width * 0.5f )(effectively divide by 2 but technically faster)
50.0f is half of the width of your gui label, which was 100.0f
next we have the height and we are doing the same but without reducing the height by half before minus-ing that from itself.
Anyway, its a start, hope that helps. take care bud Gruffy
No worries dude, please ,mark it as an answer though, so that other may find this resolve when/if they too come across the same issues. Thanks and take care. Gruffy
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Trouble with OnCollisionEnter and Exit 1 Answer
Guidance to read write and learn Programming 3 Answers
On Button Press, Show GUI Window 2 Answers