- Home /
Score with gui-Text
I need help with scoring in gui-text. I'm trying to make the gui-text count up when it hits a cube. I'm having trouble with this script.
var Counter : int = 0;
function OnCollisionEnter (myCollision : Collision) { if(myCollision.gameObject.name == ("Cube")); Counter++;{ guiText.text : "Score: "+Counter; } }
Please help.
Answer by oliver-jones · Nov 26, 2010 at 05:12 PM
Clean up of your code:
var Counter : int = 0;
function OnCollisionEnter (myCollision : Collision) { if(myCollision.gameObject.name == ("Cube")) { Counter++; guiText.text : "Score: "+Counter; } }
Try that, your code was messy, so I believe I have put things in the right place. Give it ago
---EDIT--- Okay, for this, I would suggest making 2 scripts, 1 where all your GUI functions go, and another for the cube.
Script GUI:
function OnGUI(){
guiText.text : "Score: "+CubeScript.Counter;
}
CubeScript:
static var Counter : int = 0;
function OnCollisionEnter (myCollision : Collision) { if(myCollision.gameObject.name == ("Cube")) { Counter++; } }
Place the ScriptGUI into a game empty within your scene. For now on: Everything to do with your GUI should be placed within that script - makes everything really easy.
Thank you, that sort of helped, the eror mesages are gone. but the script won't work.
What are you trying to get the script to do? If you want to display GUI text then you need to put it into a function OnGUI, and then place this function on top of a game empty - I'll update the post>
It all works but the counter wont count up when it hits the cube.
Does the variable 'count' itself move? Print the count variable and see if it goes up. If it doesn't then you have a problem in your count script somewhere - hard to tell as you haven't uploaded it
Answer by Jesse Anders · Nov 26, 2010 at 04:27 PM
Please use code formatting (the little button with 1s and 0s on it) when posting code. It doesn't always work correctly (IMX), but it will be better than what you have now. (You can edit your post and make this change.)
In any case, it looks like you have an errant semicolon after your 'if' statement. I'm not quite clear on what happens after that though. Is this your actual code? If so, does it compile, or are you getting compiler errors? (You can edit your original post to add additional information as needed.)
I can't find a button with 1's and 0's on it. what are you talking about
It's probably no longer relevant for this thread, but for future reference, when you create or edit a post, there should be a row of buttons/controls right above the content area. One is a little icon of the earth with an arrow (hyperlink), one is a double-quote mark (quote), and one has 1s and 0s on it. The one with 1s and 0s is the one to which I'm referring.
Answer by Jay_Adams · Nov 27, 2010 at 01:07 AM
I've been having problems getting this to work for about a week.
Just wanted to say thanks Oliver, I'm now up and running.
@Jesse,
I don't know if it's required, but I also put a component/rendering/GUIText onto my ingame object that contains GUI stuff (my health meter [guitexture]) That let me type, pick font, size, and place it on screen where I wanted.
Olivers "score" showed in game instead of the text I wrote (a cuss word, lol), but in the same spot/font/etc..
(I think if you don't put the guiText on there you have to specify placement/font via script, not as easy IMO)