- Home /
No position variable for GUIText()?
public class OverHeadEffects: MonoBehaviour {
GUIText text;
public OverHeadEffects(Vector3 pos)
{
this.gameObject.transform.position = new Vector3(pos.x, pos.y+10f, pos.z);
}
public void setTextColor(string text, string color = "#000000"){
this.text = new GUIText();
this.text.richText = true;
this.text.text = "<color=\"" + color + "\">" + text + "</color>";
}
public void setText(string text){
this.text = new GUIText();
this.text.text = text;
}
public string getText() { return this.text.text; }
}
...I'm guessing there's something I'm missing about the way things interact in Unity. Basically, what I'm trying to make is a class which, when initialized by the character object, creates itself slightly above the character/enemies, the prints out a text string (in this case, it's supposed to appear to show damage taken/dealt). However, GuiText has no transform variable, so I guess I'm a little confused. Do I need to attach it to an existing GameObject as a variable? How would I best do this?
Answer by robertbu · Jun 08, 2013 at 02:06 PM
GUIText does have a transform. You can access it by:
text.gameObject.transform.position = new Vector3(0.5, 0.5, 0.0);
But there is an issue with respect to placing GUIText adjacent to a game object. GUIText lives in Screen space. Most game objects live in World space. Screen coordinates start at (0,0) in the lower left of the screen and go to (1,1) in the upper right of the screen. In order to make the conversion you can use [Camera.WorldToScreenPoint()][1]
. So you would do something like:
Vector3 v3ScreenPos = Camera.main.WorldToScreenPoint(v3WorldPosition);
text.gameObject.transform.position = v3ScreenPos;
[1]: http://docs.unity3d.com/Documentation/ScriptReference/Camera.WorldToScreenPoint.html
Answer by DricoJD · Jun 08, 2013 at 01:58 PM
I would not use Gui-Text as they can only really function very well inside of a GUI Texture (Atlas). Try using 3D Text it is the same as a GUI text, but moveable.
The big difference is "lives in the world" vs. "lives in screen." A 3DText naturally shrinks with perspective and can be viewed from any angle. Oddly, 3DText is not normally hidden by things in front of it, but you can make it that way.
Both can have problems with looking bad, which can be fixed with a little work.
Your answer
![](https://koobas.hobune.stream/wayback/20220613103749im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Targets 1 Answer
audio.Play() not working? 6 Answers
GetComponent, int error, if statement, problem. 1 Answer
how to make fixed guiText ? 0 Answers
Access guiText from other object. 3 Answers