Null Reference with ifstatements accessing enum types.

Hello everyone.

So I have this problem with my interaction script.
Here’s my code:

if(whatIHit.collider.tag == "Interactable")
            {
                if (Input.GetKeyDown (KeyCode.E))
                {
                    if (whatIHit.collider.gameObject.GetComponent<MeleeWeapons>().whatItemAmI == MeleeWeapons.meleeweapon.Iron_Sword)
                    {
                        Destroy(whatIHit.collider.gameObject);
                        Debug.Log("You just picked up a " + whatIHit.collider.gameObject.name);
                    }
                    else if (whatIHit.collider.gameObject.GetComponent<MeleeWeapons>().whatItemAmI == MeleeWeapons.meleeweapon.Wood_Sword)
                    {
                        Destroy(whatIHit.collider.gameObject);
                        Debug.Log("You just picked up a " + whatIHit.collider.gameObject.name);
                    }
                    else if (whatIHit.collider.gameObject.GetComponent<MeleeWeapons>().whatItemAmI == MeleeWeapons.meleeweapon.Mace)
                    {
                        Destroy(whatIHit.collider.gameObject);
                        Debug.Log("You just picked up a " + whatIHit.collider.gameObject.name);
                    }
                    else if (whatIHit.collider.gameObject.GetComponent<Potions>().whatItemAmI == Potions.potions.LargeHealth)
                    {
                        Destroy(whatIHit.collider.gameObject);
                        Debug.Log("You just picked up a " + whatIHit.collider.gameObject.name);
                    }
                    else if (whatIHit.collider.gameObject.GetComponent<Potions>().whatItemAmI == Potions.potions.MediumMana)
                    {
                        Destroy(whatIHit.collider.gameObject);
                        Debug.Log("You just picked up a " + whatIHit.collider.gameObject.name);
                    }
                    else if (whatIHit.collider.gameObject.GetComponent<Keys>().whatItemAmI == Keys.keys.chestKey)
                    {
                        Destroy(whatIHit.collider.gameObject);
                        Debug.Log("You just picked up a " + whatIHit.collider.gameObject.name);
                    }
                }
            }

So this let’s me pick up all the meleeweapons with no errors, but when i pick up a potion or the key, it throws me a null reference error to the first if statement.
So the sword has a script on it called MeleeWeapons, the potions have one called Potions and the key has one called Keys.
Each script has an Enum with different types of the item reffered to.
Can I not check for different components in the same if statement?

GetComponent<MeleeWeapons>() will be null if there is no MeleeWeapons component on the object.

Check if it is null to choose:

if (thingamajig.GetComponent<MeleeWeapon>() != null) {
    Debug.Log("got weapon, do other ifs.");
} else if (thingamajig.GetComponent<Potion>() != null {
/* Potion, etc */
}

I would personally recommend you get the component once at the beginning, and then do your work. It’ll make things clearer to read at minimum.

Thank you for the answer :slight_smile: