Put a GUITEXT into the middle of an object.

Hello guys,

I got a little problem. I would like to add a text into a box, that I can change dynamically with C#.

It kinda looks like that:

10940-textbox.png

The GUITEXT elements and my “box” are children of the camera I’m using.

I got a funktion that shows me the textbox. It works perfektly one time…
If I try to use this function a second time the text is somewhere in the world where it isn’t supposed to be. Or at least where I don’t want it to be.

What am I doing wrong? Here is an example of code I am using:

Vector3 screenPos = Camera.main.WorldToScreenPointMyTextBox.transform.position);
		float x2 = screenPos.x / Screen.width;
		float y2 = screenPos.y / Screen.height;

		MyText.transform.position = new Vector3(x2+0.02f,y2+0.13f,0.98f);

I guess I mess up the coordinates somehow, but where?
Why does it work the first time but won’t work right the second time?
Is it because the objects are children of the main camera?

Thanks for any help!

Yes, I think making the GUI Text a child of your camera will mess up it’s transform. The GUI Text is placed in screen space, so there is no need for it to be a child of the camera, so let it be unparented. Your box on the other hand, assuming you want it to stay in the same place relative to your camera, should be a child of the camera.

If your box never moves relative to the camera, you should be able to setup your GUI Text to display over the box inside the Unity Editor and never need to place it in script. If your box ever moves or you window size may change, then you may want to re position your GUI Text.