Scripting GUIText

Hello again everyone. i am back with yet another problem :frowning: I am trying to create some text at runtime. I dont really understand the GUIText fully, but here is what i have written so far, and nothing is rendered whatseoever in my scene.
I do not have any other objects in the scene other than the MainCamera object, cause everything im doing is from script.

Not sure if this is part of he problem or not. But from what i have read, it seems that as long as camera has a GUILayer component, than all GUIElements in the scene should be able to get rendered. Anyways , if anyone understands or can help out with a few tips, i greatly appreciate it, thanks you!

private var clock : GameObject;
private var clockText : GUIText;

function Start()
{
	clock = new GameObject();
	clockText = clock.AddComponent(GUIText);
	clockText.font = Resources.Load("BaroqueScript");
	clockText.fontSize = 42;
	clockText.material.color = Color.red;
	clockText.text = ("tester");
	clockText.enabled = true;
	clock.transform.position.x = 0;
	clock.transform.position.y = 0;
	clock.transform.position.z = 0;
}

I think you have a positioning problem. Remove all those clock.transform.position lines and try adding this instead and your code should work, assuming you have a BaroqueScript asset in your project:

   clockText.transform.position = new Vector3 (0.5, 0.5, 0.5);

uhhhh … wow … but how?? please explain. Does this have something to do with a depth setting when using 2d gui ? I have left my transform code in place, it does the same thing Vector does, just longer to type, so the only thing possiby different is the positioning? And it works perfectly at the coords you provided

ps : either way … thanks alot :smile:.

Nonono. :slight_smile:

(0.5, 0.5, 0) or (0.5, 0.5, 1) or (0.5, 0.5, 100) will all look the same as the depth doesn’t matter with 2D GUI elements. When you set the position (0 <= position <= 1), you’re telling it where on the screen (not the game world) the TOP LEFT corner of the text should be. Some interesting examples…

(0,1,0) : Text at top-left and appears fine.
(0,0,0) : Text at bottom-left but you can’t see it because it’s too low. (0, 0.1, 0) will make it visible.
(1,0,0) : Text at bottom-right, but you can’t see as it’s too far to the right. (0.9, 0.1, 0) makes it visible.
(1,1,0) : Text at top-right, but you can’t see it because it’s too far to the right. (0.9, 1, 0) will make it visible.

(0.5, 0.5, 0.5) was a quick way to get it smack dab in the middle.

Now I’ll cover a more graceful solution to give you a better understanding. The reason (0,0,0) sets the TOP-LEFT corner of the text to be at 0,0,0 is because GUIText.Anchor=“upper left” by default.

We could make (0,0,0) work by doing it this way:

	clockText.anchor = TextAnchor.LowerLeft;
	clock.transform.position = new Vector3(0,0,0);

Similarly, a nice way to make (1,0,0) work would be this way:

	clockText.anchor = TextAnchor.LowerRight;
	clock.transform.position = new Vector3(1,0,0);

And by now you should be a master of GUI positioning. :slight_smile:

Sweet, thanks for your replies and explanations… that helped alot, now i understand and it all works great. :stuck_out_tongue: thanks you muchly!

PS : Maybe you would have a look at the two other posts i have and see if you can spot the issues within? lol, but again, thanks alot, you were a huge help.