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;
}
}
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.
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
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.