- Home /
GUIText doesn't appear in Game View
Hi, my score in GUIText doesn't appear in my Game view. My game is 3D. Should I use GUITexture or 3D Text instead? If yes, how should I edit my original script:
#pragma strict
private var score : int = 0;
var textScore : GUIText;
function Start () {
textScore.text = "Score: 0";
}
function Update () {
}
function OnCollisionEnter (col : Collision) {
if (col.collider.name == "Cylinder1") {
Destroy (col.gameObject);
score += 50;
textScore.text = "Score: " + score;
}
}
Do you have a GUIText object in the scene which you've used to initialize textScore? Also note that GUIText objects live in Viewport space. To be visible, their x and y positions must be between 0 and 1.
Answer by Eric5h5 · May 26, 2013 at 05:11 PM
A GUIText object uses viewport space, so the x/y coords must be in the 0..1 range. Any camera that you want to display the GUIText object needs to have a GUILayer component.
Answer by JCprogrammer · May 26, 2013 at 04:52 PM
when you add your code to a GameObject , it will appear on that gameobject's inspector . there you need to add the gameobject which carries the GuiText component . if you have not set any GuiText Component to any gameobject on the scene , then add one from Component ==> Rendering ==> GuiText to , for instance , the same gameobject which carries your javascript code . then add this line :
textScore = this.guiText;
to the start function of you code , which then set the gui component to the variable you set up there automaticly , to work with it
Answer by kaliban · May 26, 2013 at 04:36 PM
The easiest way to display current score IMO is to use OnGUI()
For example
void OnGUI() {
GUI.Label(new Rect (10,10,150,20), "Score: "+score);
}
This will display text in the left upper corner of the screen. And when variable score changes, it will be immediately displayed.
Using OnGUI ins$$anonymous$$d of a GUIText object has several drawbacks, namely that it's updated every frame whether you need it to or not (therefore wasting CPU time) and there's no simple way of making it resolution-independent (you have to mess around with GUI.matrix).
Your answer
