Keep track on score when objects are destroyed.

In my game player gets points when he makes the 2 balls collide with each other. When the balls collide, both of them are destroyed. I know there has to be a simple way to count the score that I just didn’t realize. OnDestroy or OnDisable won’t work. Simply can’t find a way.

Create simple empty GameObject and attach “singleton” element to it. Hers example of the script:

public class ScoreCounter : MonoBehaviour {
    private int m_Score = 0;
    private static ScoreCounter m_Instance;

    public int Score
    { get{ return m_Score; } set { m_Score = value; } }
    public static ScoreCounter Instance
    { get{ return m_Instance; } }

    public void Start(){
        if(m_Instance != null){
            Destroy(this.gameObject);
            return;
        }
        m_Instance = this;
    }
}

Then in your ball script create method OnDestroy();

public void OnDestroy(){
    ScoreCounter.Instance.Score++;
}

Hope that will help.