I am making a top-down shooter type of game in Unity. The player can move in the world, but the camera moves with him, so the player is always in the middle of the screen.
I'm trying to display some text in the upper-left corner of the screen, and so I attached a GUItext component to the main camera. The result was quite interesting - the moment the player moves the text darts off the screen.
What the text is supposed to display is controlled by the player, so I added the following snippets of code in my player script:
public GUIText scoreText;
void Update ()
{
scoreText.pixelOffset = new Vector2 (300, 300);
}
void UpdateScore ()
{
scoreText.text = "Lives: " + lives;
}
but that didn't change anything, and the void UpdateScore ()
didn't work either.
I am rather new to Unity, so this is probably a very simple mistake... but what am I doing terribly wrong?
EDIT: the void UpdateScore ()
works now that I call from void FixedUpdate ()
.
EDIT (again): I fixed the problem by making a new Empty Object with nothing in it except the transform (which never moves) and a GUI text component. The problem I have now is that the anchor on the GUI text works very strange - it only works if I put it on the lower-left (whereas I want it on the upper left). Trying to change the anchor moves it somewhere off the screen. Here's my hierarchy:
Make sure that your GUIText component is attached to its own GameObject that does not move. I would guess that right now it is on your player or the camera.
(When the GameObject the GUIText component is attached to moves, the onscreen position of the displayed text also moves.)