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));
}
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
screen.width (the full width of screen)
minus
half of the screen width (Screen.width * 0.5f )(effectively divide by 2 but technically faster)
the
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