Relative GUIText position using screen coordinates

Hi guys. I’m not sure if its possible to position a GUItext using screen size / coordinates. By that i mean using something like Screen.width / 2, Screen.height / 2 kinda thing like you can use for buttons etc. In the inspector i can see that the position is 0 -1 for it to be visible on the screen, anything over or under that is disappears entirely form view. As far as i know any GUI objects are placed using pixel or screen coordinates? The text im displaying changes depending on what time you finish the level, so sometimes its longer or shorter and i want the text to regardless of size always be in the middle of the screen. this is the code im working with.

var TimerAccess : GameObject ; // object script is attached to
var timeAccess : LevelTimer ; // script variable is attached too
var Score : GUIText;
var OneStar : GUITexture;
var TwoStars : GUITexture;
var ThreeStars : GUITexture ;


function Start (){
	
	OneStar.enabled = false;
	TwoStars.enabled = false;
	ThreeStars.enabled = false;
	Score.text = null;
	timeAccess = TimerAccess.GetComponent(LevelTimer);
}

function OnControllerColliderHit (hit : ControllerColliderHit){

	if (hit.collider.gameObject.name == "EndLevelTrigger")
	{
		Debug.Log("Hit trigger");
		LevelTimer.LevelOneEnd = true ; // show buttons on other script.
		// Temporarily disable controls
		GetComponent(MouseLook).enabled = false;
		GetComponent(CharacterController).enabled = false ;
		GetComponent(FPSInputController).enabled = false ;
		GetComponent(CharacterMotor).enabled = false ;
		
		if (timeAccess.timecount < 60) // script you want to access (timeAccess) and variable (timecount) to want to compare. 
		{
			Score.transform.position = Vector3 (Screen.width /2, Screen.height / 2, 0);
			Score.text = "Excellent work" ;
			ThreeStars.enabled = true;
			Debug.Log("3 stars" );
		}

when i use the score.transform .position and try to place it using screen coordinates it disappears from view. Let the help commence :slight_smile:

Hi p_unity,

I had some trouble finding GUITexture & GUIText relative position and size examples using transform position properties between 0 and 1 in the documentation too.

I add GUIText and GUITextures to the scene and set transform position and scale properties to between 0 and 1 in the inspector (mostly 0 for viewport bottom left, coordinate style), later those properties/members are manipulated at runtime by unity script in the game. This method seems to work very well for me in terms of cost vs performance as opposed to the horror of ONGUI :smiley:

This my start function for GUIText on the health bar in my android game for relative sizing and positioning of that GUIText on start.

function Start () {
    GUIText.pixelOffset.x = Screen.width/2; // if your gui text transform positions are set to 0 this will be in the middle of the view
    GUIText.pixelOffset.y = Screen.height/15; // near the bottom of the rendered view
    GUIText.fontSize = 0.1; // Seems to make font size behave by viewport paradigm and become relative sized.
    // GUI layer stack order is controlled by transform.position.z on GUI Text and GUI Textures in case you want to stack them together.
} 

I hope that helps a little :slight_smile:

Don’t place using transform.position. You want the X,Y coordinates to be 0.5, 0.5. To keep it centered you want to anchor it to Middle Center and Align it to center. Both those properties can be accessed in code like this:

score.anchor = TextAnchor.MiddleCenter;
score.alignment = TextAlignment.Center;

Or you can simply set them in the inspector.