Search code examples
unity-game-enginecollisioncollider

How can i detect a collision to add score?


When my player touches the object, it should add score but does not go. I put this code on the tree. Thanks!

public class LifeTree : MonoBehaviour
{   
    private int contador=0; 
    private ScoreData _scoreData; 

    private void OnCollisionExit(Collision other)
    {
        if (CompareTag("Player") & contador < 4)
        {
            _scoreData.score = _scoreData.score+5;
            Debug.Log("Tree"); 
            contador++;   
        }
    }
}

Solution

  • So first make sure a rigidbody is attached so a collision can be recognized.

    void OnCollisionEnter(Collision col)
    {
      if(col.gameObject.tag == "Player" && contador < 4)
      {
        //detects if the player hit the tree
      }
    }