Collision and score

I have this script that contains my score, this script was downloaded from a tutorial and comes with the action of pressing the b key and it adds the player 100 points, i want to change that so that when the player collides with the tag Coin it adds up 100 points and then it destroys the abject with the tag Coin. This is the code:

var score1 : GUIText;
var myScore : int = 0;
var myCurScore : int = 500;
 
function Update () {
 
 
score1.text = "Score: " + myScore;
 
 
if(myScore < myCurScore) {
 
myScore += 10;
 
 
}

if(Input.GetKeyDown("b")) {
myCurScore += 100;
}
},

1 Answer

1

Reading your question, I think you just came here and posted this without trying to do it before. However, from the documentation: OnCollisionEnter()

Just write it down like this:

function OnCollisionEnter(collision : Collision) {
   if (collision.collider.name == "Coin")
      myCurScore += 100;
}

Note that collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached.

Thanks, but i have tried with on trigger enter

Did you add the rigidbody to the object as I said before?

yes, but its still not working, i put to the player object and to the Coin object