scoring

iv been trying to make a simple scoring system

when enemy collides with bullet score = +10

iv been looking around for some directions or snippets and have found a few, but they all display the score in the consol

does anyone know a simple way to display a score in a gui text?

any help is greatly appreciated

thanks in advance

just use a GUI label and update it as the score increases.

do you know how id go about doing that?

i have no idea sorry >.<

function OnGUI () 
{
	GUI.Label (Rect (10, 10, 100, 20), "Score: " + MyScoreCounter);
}

ok thanks, but i cant get it working >.<

heres what i have

 static var MyScoreCounter : int = 0;

function OnCollisionEnter()
{
	if (gameObject.tag == "Bullet"){
	Destroy(gameObject);
	MyScoreCounter += 10;
	}
}

which i thought would mean, if the bullet collides with the object the script is on, +10 on score counter

 function OnGUI () 
{
	GUI.Label (Rect (10, 10, 100, 20), "Score: " + MyScoreCounter);
}

have i done something terribly wrong? >.<

Are those two code excerpts part of the same script, or part of different scripts? And what game object(s) are they associated with?

both are different scripts, by my understanding the first script is placed on the object you want to be the trigger the “enemy” and the second is what displays the score, iv placed this on the camera

have i done something wrong? >.<

Is the first script attached to the ‘enemy’ objects, or to the objects that are supposed to collide with the enemy objects?

the enemies object, sorry

It looks to me like you have the score associated with each individual bullet. When a bullet collides, it increases the score (only saved to that one bullet) and then destroys itself. Your GUI call then never references the object that MyScoreCounter is attached to.

What you probably want to do is have another script on the player called PlayerScore with a static MyScoreCounter variable. that way, each time a bullet collides, it says

PlayerScore.MyScoreCounter += 10;

Do the same for the GUI call (PlayerScore.MyScoreCounter) and it should work better. Assuming that was your problem to begin with.

thanks for the help but my problem is that the score doesn’t display at all, the value doesn’t change

Is it white text on a white background? Is the script attached to an object so it gets called?

the text is displayed as

Score:

when the bullet collides with the enemy, what should happen is it then says

Score: 10

but it just remains as Score:

The score variable is already static; as such, it is not associated with any particular instance of the script.

@The OP: You said the first script is attached to the ‘enemy’ objects, but the tag you’re checking is the tag for the object to which the script is attached. What you probably want to do is check the tag for the object that’s just been collided with, which you can access (indirectly) via the (optional) argument to OnCollisionEnter() (check the docs for details).

avoid this: static var MyScoreCounter : int = 0; it´s not a clean solution…
attach something like a levelcontroller which manages this part of your game and use sendMessage to send the value … this is always a cleaner solution and you won´t loose score points!
in this method it´s could happen that your score value isn´t send to your scorecounter before your object gets destroyed. it´s possible that your object hits your enemy but counts up no score points. believe me, i tryed this way and it´s not a clean solution:) first make sure that your points were “delievered”…after that: destroy your object.

do you have an example you could show me please?

I’d recommend getting your current version working before you try to change the whole architecture. (As I said in my earlier post, I suspect the problem is that in your first script, you’re checking the wrong tag.)

im certain the tags are correct

this is exactly what im trying to run

Counter.js

static var MyScoreCounter : int = 0;

function OnCollisionEnter(Bullet : Collision)
{
	if (gameObject.tag == "Bullet"){
		MyScoreCounter += 10;
	Destroy(gameObject);
	}
}

this scripts attached to the enemy

MyScoreCounter.js

 function OnGUI () 
{
	GUI.Label (Rect (10, 10, 100, 20), "Score: " + MyScoreCounter);
}

this scripts attached to the player camera

i really cant see where iv added the wrong tag, or if iv miss spelt anything, should these scripts work?

sorry i haven’t got a clue >.<

That’s not what I mean. I mean that you’re checking the tag for the game object that the script is attached to, not the game object that that object has collided with. In other words, you’re checking the tag for the wrong object.

These problems are generally easier to solve if you use the debugger (in 3.x) or add debug output using (e.g.) Debug.Log() to determine what’s going wrong. For example, in this case you might add the following output (untested):

function OnCollisionEnter(Bullet : Collision)
{
    Debug.Log("OnCollisionEnter() called...");
    Debug.Log("Tag we're checking is " + gameObject.tag);
    if (gameObject.tag == "Bullet"){
        Debug.Log("Increasing score");
        MyScoreCounter += 10;
        Destroy(gameObject);
    }
}

The output should then give you a hint as to what’s wrong.

At least one of your objects needs a rigidbody attached to it for OnCollisionEnter to work properly.

Also, your GUI script isn’t going to know where to grab the variable ‘MyScoreCounter’.

You should declare it in that script as static, and then call it from other scripts as scriptName.MyScoreCounter, replacing ‘scriptName’ with whatever you named your GUI script.

Also, rather then apologizing for not having a clue, check the many tutorials and reference manuals here:

http://unity3d.com/support/documentation/

And if you’re new to scripting, see this:

http://forum.unity3d.com/threads/34015-Newbie-guide-to-Unity-Javascript-(long)