Slots Manager

Hi guys i’ve made a slot changer script but i’ve problem
Error : Object reference not set to an instance of an object
it’s tell me there’s no gameObject attached and i’m checking if the gun is attached to script by doing this :

            if(gun.Weapon && CurrentSlot == gun)
            {
                gun.Weapon.SetActive(true);
            }
            else{
                gun.Weapon.SetActive(false);
            }

So you check if gun.Weapon exists in the first line, as well as if CurrentSlot is gun.

Note though that in your else, you still access gun.Weapon. Thing is, you’ll end up in that code if gun.Weapon is null… when you access a null reference you’ll get the exception:

“Object reference not set an an instance of an object”

How about you try this:

if(gun.Weapon)
{
    if(CurrentSlot == gun)
        gun.Weapon.SetActive(true);
    else
        gun.Weapon.SetActive(false);
}

Oh thanks it’s working
I thought about it, but i didn’t knew that it work
ty btw :slight_smile: