How to calculate different amount of points for different type of items

Hi,
I need an answer how to calculate different amount of points for different type of items.

For example, I have "Coin" and "GoldenCoin" - for coin i add to score +1 and for goldencoin i want add to score +2. But how?

Here is my script (second part of script,which about overriding,doesent work tho)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Coin : MonoBehaviour
{
    protected int Score = 0;
    public Text Text;

    public virtual void OnTriggerEnter(Collider other)
    {
        if(other.gameObject.tag == "Coin")
        {
            Score++;
            Destroy(other.gameObject);
            Text.text = Score.ToString();
        }
    }
}
public class GoldenCoin : Coin
{
    public override void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "GoldenCoin")
        {
            Score = Score + 2;
            base.OnTriggerEnter(other);
        }
    }
}

Your Code could be made more modular such as, however there are still better ways to handle this.
Some tips,

  • Try to avoid using gameObject.tag == "..", instead use gameObject.CompareTag("")
    accessing gameObject.tag will create garbage because it is a C++ function (extern call)

  • Rather than Text.text = Score.ToString(), use Text.SetText("{0}",Score);
    This will minimal GC due to string creation toString()

public class Coin : MonoBehaviour
{
public void OnTriggerEnter(Collider other)
    {
        if(other.gameObject.tag == "Coin")
        {
            OnPickedUp(1);
        }
}
protected void OnPickedUp(int scoreAdd)
{
Score += scoreAdd;
Destroy(other.gameObject);
            Text.text = Score.ToString();
}
    }

public class GoldenCoin : Coin
{
    public void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "GoldenCoin")
        {
           OnPickedUp(2);
        }
    }
}