x


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.

more ▼

asked Nov 26, 2010 at 04:16 PM

Spencer 1 gravatar image

Spencer 1
1 7 6 6

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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.

more ▼

answered Nov 26, 2010 at 05:12 PM

oliver-jones gravatar image

oliver-jones
3.9k 716 1191 696

Thank you, that sort of helped, the eror mesages are gone. but the script won't work.

Nov 26, 2010 at 06:58 PM Spencer 1

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>

Nov 26, 2010 at 07:37 PM oliver-jones

For the moment it says it's still compilling

Nov 30, 2010 at 07:04 PM Spencer 1

It all works but the counter wont count up when it hits the cube.

Nov 30, 2010 at 10:03 PM Spencer 1

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

Nov 30, 2010 at 10:10 PM oliver-jones
(comments are locked)
10|3000 characters needed characters left

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.)

more ▼

answered Nov 26, 2010 at 04:27 PM

Jesse Anders gravatar image

Jesse Anders
7.7k 12 30 64

I can't find a button with 1's and 0's on it. what are you talking about

Nov 30, 2010 at 06:46 PM Spencer 1

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.

Nov 30, 2010 at 07:01 PM Jesse Anders
(comments are locked)
10|3000 characters needed characters left

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)

more ▼

answered Nov 27, 2010 at 01:07 AM

Jay_Adams gravatar image

Jay_Adams
67 8 7 12

Could you show me your script

Nov 30, 2010 at 06:54 PM Spencer 1
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x65
x40

asked: Nov 26, 2010 at 04:16 PM

Seen: 19513 times

Last Updated: Nov 26, 2010 at 04:16 PM