Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I use Enum in switch statement [Unity c#]

What I am trying to do is detecting the type of "item" and using it inside of a switch statement and based on that, choosing correct equipment slot.
My code:

    public EquipmentSlot WeaponSlot;
    public EquipmentSlot ChestplateSlot;
    public EquipmentSlot LeggingsSlot;
    public EquipmentSlot HelmetSlot;

    public void EquipItem(Item item)
    {
        EquipmentSlot chosenSlot = null;
        
        switch(item.type)
        {
            case item.type.weapon:
                chosenSlot = WeaponSlot;
                break;
            case item.type.chestplate:
                chosenSlot = ChestplateSlot;
                break;
            case item.type.leggings:
                chosenSlot = LeggingsSlot;
                break;
            case item.type.helmet:
                chosenSlot = HelmetSlot;
                break;
        }

        chosenSlot.EquippedItem = item;
    }

Item class:

    public enum Type
    {
        weapon,
        chestplate,
        leggings,
        helmet
    }
    public Type type;

This error is showing up:
error CS0176: Member ‘Item.Type.helmet’ cannot be accessed with an instance reference; qualify it with a type name instead
(four times for each case in switch)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Instead of case item.type.weapon: it should be case Item.Type.weapon:

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading