- Home /
GUI Text Problem
Hello,
I have been having some issues with my GUI text. As you can see from the script i would like the Gui text to update with the counter. var Counter : int = 0; private var lastCube : GameObject; function OnCollisionEnter (myCollision : Collision) { if(myCollision.gameObject.name == "Cube" && myCollision.gameObject != lastCube){ Counter++; guiText.text = "Score : " +Counter; lastCube=myCollision.gameObject; } }
I have attached this script to the projectile that will hit the cube, i receive no console errors and i have attached the textgui to the projectile. Any help is much appreciated.
Just to be sure, which language are you using? Javascript? C#?
I think you need to use the OnGUI function to show text on the GUI.
Answer by Molix · Aug 26, 2011 at 01:59 AM
The issue is that the GUIText Component uses a special normalized coordinate system, meaning for it to show on the screen its x and y coordinates need to be between 0 and 1 (e.g. 0.5, 0.5 is the middle of the screen). Since you have attached it to the projectile, that is unlikely. Instead, put the GUIText in a separate GameObject that doesn't move. You will have to get a reference to that new object so you can tell the score to change, but there are several approaches to that (e.g. keep a reference, Find(), etc).
Answer by aldonaletto · Aug 26, 2011 at 02:22 AM
guiText is meaningful only if the object is a GUIText. Assigning the GUIText to the projectile makes it a child, what doesn't set the projectile's variable guiText.
Another problem: the variable lastCube exists in each projectile you instantiate; if you create a new projectile each time you shoot, lastCube will always start with the null value.
A simple way to make this work is to create a script for the GUIText (let's call it GuiScore.js), place the variables lastCube and Counter in it and declare them as static, so they can easily be accessed from the projectile script:
static var lastCube: GameObject; static var Counter: int = 0;
function Update(){ guiText.text = "Score : " +Counter; } Then change the projectile script to:
function OnCollisionEnter (myCollision : Collision) { if (myCollision.gameObject.name == "Cube" && myCollision.gameObject != GuiScore.lastCube){ GuiScore.Counter++; GuiScore.lastCube = myCollision.gameObject; } }
Your answer
Follow this Question
Related Questions
GUI Text Blinking came up with one error. 3 Answers
Change text gui.button in a for bucle 1 Answer
Displaying a collision with Obstacle with text on screen? 0 Answers
converting from GUI text to GUI Labels 0 Answers
Instantiating object from inventory 0 Answers