C# switch Unity

using UnityEngine;

public enum CollectItemType { jump, ammo, health, weapon}


public class CollectableItem : MonoBehaviour
{
    [SerializeField] CollectItemType collectItemType;
    
    public CollectItemType GetCollectItemType()
    {
        return collectItemType;
    }
}
using UnityEngine;

public class Player : MonoBehaviour
{
    
    void ActivateTripleJump() { Debug.Log("You can make tripple jumps now"); }
    void AddWeapon() { Debug.Log("You can use a new weapon now"); }
    void AddAmmo() { Debug.Log("Ammo added"); }
    void AddHealth() { Debug.Log("Hp increased"); }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        CollectableItem collectableItem = collision.GetComponent<CollectableItem>();
        if(collectableItem != null)
        {
            CollectItemType ctype = collectableItem.GetCollectItemType();

            switch (ctype)
            {
                case CollectItemType.jump:
                    ActivateTripleJump(); 
                    break;
                case CollectItemType.weapon:
                    AddWeapon();
                    break;
                case CollectItemType.ammo:
                    AddAmmo();
                    break;
                case CollectItemType.health:
                    AddHealth();
                    break;
                default:
                    break;
            }
        }
    }
}
Scroll to Top