- Home /
Score updating after collision
Hi, I'm a total beginner to Unity and I just needed some help with scoring. At the moment what I've got is this:
var Counter : int = 0;
function Update () {
guiText.text = "Score: "+Counter;
Counter+=10;
}
Which updates the score constantly, but what I want is for the score to increase by 10 when there is a collision between two objects. Is there anyway I can specify this? Thanks in advance.
Update: Yay, I figured it out! Basically I wanted the score to increase whenever the basketball hit the target. This is the code I've now got attached to the score:
var Counter : int = 0;
function Update () {
guiText.text = "Score: "+Counter;
}
function React () {
Counter++;
}
And this code attached to the basketball:
function OnCollisionEnter(theCollision : Collision){
if(theCollision.gameObject.name == "Target"){
gameObject.Find("Score").SendMessage("React");
Debug.Log("Score!");
yield WaitForSeconds(1);
gameObject.active=false;
}
}
Thanks for the help!
Updated answer with correct code. Also, for reference, next time ins$$anonymous$$d of creating a new answer, simply comment on an old one. :)
Answer by Apples_mmmmmmmm · Jan 06, 2012 at 03:08 AM
Make a Scorecounter script and attach this to your gui.Text object.
static var Counter : int = 0;
function Update () {
guiText.text = "Score: " + Counter; }
Make a scoretrigger script and attach this to the trigger that you want the ball to enter. Make sure that the checkbox 'isTrigger' on the gameobject is checked. Make sure that the object you're using as a ball is marked with a tag called ball (or if called something else, just change the CompareTag string to the tag's name).
function OnTriggerEnter(enterer : Collider)
{
if (enterer.collider.gameObject.CompareTag("ball"))
{
Debug.Log("Score!");
Scorecounter.Counter ++;
}
}
This is really usefull, thanks guys.
But how would I go about respawning my ball on collision?
Thanks,
Your answer
Follow this Question
Related Questions
how to keep track of bricks broken 1 Answer
Scoring (i'm stuck) 3 Answers
Score count increase on hit 2 Answers
Collision with cube isn't working when Instantiate clone it 2 Answers
Destroy object after 5 collisions 1 Answer