- Home /
GUIText viewporttoworldpoint makes text disappear.
Hi All. I've created a GUIText, whoch defaults to the center of my screen. I know I can move it around using the Pixel Offset, but I want it to be consistant regardless of the aspect ratio, so I added the following script to another gameObject, referencing the GUI:
var clipDisplay : GUIText;
var clipDisplayX = 0.5;
var clipDisplayY = 0.5;
var thisCam : Camera;
function Update()
{
clipDisplay.guiText.transform.position = thisCam.ViewportToWorldPoint(Vector3(clipDisplayX, clipDisplayY, 0));
}
The clipDisplayX and Y are set to 0.5, the idea being I can adjust them in the Inspector window to find the position I need. But when I add the ViewportToWorldPoint line, the GUIText disappears from the game.
I know it'll be just somewhere out of sight, but I can't find it. I've adjusted the X and Y values, and added the thisCam variable to ensure it's using the right camera. I've even tried changing the font and material out of desperation, but no avail. I'm sure it's something simple I'm missing...
If I comment out that one line, the GUI comes back to the center. Strange. I'm out of ideas, can anybody help please?
Cheers
Ian
GUIText is placed at a viewport position already isn't it? Certainly is for me on U4. So just setting it's transform.position to (0.5,0.5,0) puts it in the middle of the screen.
3DText is placed using worldView coords, so you may be using code meant for one of them (ex: I want this on the ground where it appears top-centered at (0.5, 0.9) in the screen. What (x,y,z) of the terrain is currently there?)
A common use for this type of thing is on a GUIText is worldToViewPort. You know you want "Blood drinker" near the ogre's head, and need the 0-1/0-1 screen coords.
Thanks guys, like I say below, I must've been using code from an old answer from this site, as it was marked correct and voted up. Plain old clipDisplay.guiText.transform.position = Vector3(clipDisplayX, clipDisplayY, 0); works fine. Thanks again :-)
Answer by husbandofemily · Mar 23, 2013 at 12:14 PM
Whydoidoit - you're spot on. I must be using an old answer from Unity 3x.... Top Banana. I'd rate your answer right but it's in the comments section ;-) Thanks a lot.
Your answer
![](https://koobas.hobune.stream/wayback/20220613094932im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
[JS] How to display an Int variable through a GUI Text?? [Resolved] 2 Answers
How do I reference a UI text in Unity 5? (JS) 2 Answers
Score wont show 0 Answers
How to create a review of previous updates 1 Answer
Code behind 'Camera.ViewportPointToRay'? 2 Answers