- Home /
GUI Text element disappears when I am trying to change its position
I wrote the following code to keep the score of my game.
#region Score
private GameObject scoreobject;
private int score;
public void InstantiateScore()
{
score = 0;
scoreobject = GameObject.Find("scoretext");
//scoreobject.transform.position = new Vector2(0, 0);
}
public void UpdateScore(){
score++;
scoreobject = GameObject.Find("scoretext");
if (scoreobject)
{
scoreobject.guiText.text = score.ToString();
}
}
#endregion
The problem is in the commented line scoreobject.transform.position = new Vector2(0, 0);
. When I am trying to change the position of my scoreobject, the object is not appeared on the screen.
Answer by robertbu · Mar 02, 2014 at 12:52 PM
GUIText object live in Viewport space. Viewport spaces start at (0,0) in the lower left of the screen and goes to (1,1) in the upper right. I'm assuming you want to place your object in the center of the screen. Try using Vector2(0.5f, 0.5f) for the position. Note you may also want to take a look at the anchor position for the object. For example you can select your GUIText object in the Hierarchy pane and then select 'middle center' for the Anchor.
Your answer
Follow this Question
Related Questions
Any fade-in/fade-out animations for GUI Text? 3 Answers
GUI Text For Simple Game Tutorial 1 Answer
Text Effects... 2 Answers