- Home /
GUI text not showing in scene or game view
I have set the transform positions to 0, put the text in a default layer, in the hierarchy it is not a child of the canvas. I added a script which shows a highscore, but it wont show in the game or scene view! Very annoying, I feel like I have tried everything. I see that in the text field in the inspector of the game object which has the gui text component attached, the text changes to what I want. However, it wont show in the game or scene view. i want the text to be in the center middle, with text alignment to be center as well. Heres the script im using: using UnityEngine; using System.Collections;
public class ShowScore : MonoBehaviour {
static int highScore = 0;
void Start () {
PlayerPrefs.GetInt("highScore",highScore);
highScore = PlayerPrefs.GetInt ("highScore", highScore);
guiText.text = "HS:" + highScore;
}
}
hey buddy, I see you are new to GUI. for the sake of all that is holly, don't learn it. learn unity UI, or nGUI, or anything else, but not GUI.
For any Unity 4.6 UI object to work it HAS to be under a Canvas. And the alignment should be very easy to do with positioning the text in the scene view. Try starting over by trying to insert a new ui text object and work from there. Also as the guy below stated, UnityEngine.UI needs to be declared in the file too to allow you to interact with them via scripts.
Official Unity UI Tutorials: http://unity3d.com/learn/tutorials/modules/beginner/ui/ui-text
Yeah, GUIText and GUITexture are basically obsolete. Don't waste time on them.
@kastenessen you are wrong, there is so much more you can do with the new UI.
@$$anonymous$$astenessen The ui is NOT limited from scripting, the problem is the ui classes are not accessible from the default namespace imports. You have to also declare using UnityEngine.UI;
like in the answer below. Just have a quick look at the official tutorials, live training and documentation to see what is possible!
Answer by Elit3d · Jan 15, 2015 at 12:34 PM
If you are using 4.6 unity it is much easier to do it like this:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ShowScore : MonoBehaviour {
static int highScore = 0;
public Text guiText;
void Start () {
PlayerPrefs.GetInt("highScore",highScore);
highScore = PlayerPrefs.GetInt ("highScore", highScore);
}
void Update() {
guiText.text = "HS: " + highScore;
}
}
I haven't tested it so if you get any errors let me know. Just wrote it on the spot so probably has errors, maybe not. Hope this helps
EDIT: It is very important you include using UnityEngine.UI; on top otherwise you will encounter errors!
@fatmanspineapple any object you like, as long as you assign the text object to it