Error casting as WeaponConfig

I have the following error after updating to WeaponConfig : EquipableItem

Cannot convert type via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion

Went through the steps a few times and unsure where the error could be.

private void UpdateWeapon()
    {
        var weapon = equipment.GetItemInSlot(EquipLocation.Weapon) as WeaponConfig;
        if (weapon == null)
        {
            EquipWeapon(defaultWeapon);
        }
        else
        {
            EquipWeapon(weapon);
        }
    }

    private Weapon AttachWeapon(WeaponConfig weapon)
    {        
        return weapon.Spawn(rightHandTransform);
    }

The error should tell you where it is. Just click on it and look at the details.

Check that defaultWeapon is still of type WeaponConfig and the EquipWeapon takes a WeaponConfig as its parameter

The specific lines the errors are on:

var weapon = equipment.GetItemInSlot(EquipLocation.Weapon) as WeaponConfig;
 return weapon.Spawn(rightHandTransform);

Those errors suggest you have 2 Weapon scripts.

Let’s try this instead:

{
     if(equipment.GetItemInSlot(EquipLocation.Weapon) != null && equipment.GetItemInSlot(EquipLocation.Weapon) is WeaponConfig weaponConfig)
     {
          EquipWeapon(weapon);
     }
     else
     {
           EquipWeapon(defaultWeapon);
     }
}

If this construction still doesn’t work, then it’s likely that you have two WeaponConfig scripts or your WeaponConfig is not inherited from EquipableItem.

For the second error, it would appear that you have two Weapon scripts, one in the GameSystem.Combat namespace and one not in a namespace.

Yup, found it. Thank you.

After fixing the dupe weapon script, tried your constructor and still broken. I was able to fix the error by reimporting some of the equipment scripts. I’m still unsure what exactly was the issue despite the code being identical. Just one of those things that resolve itself right after posting for help after days of staring at the code. You are eternally helpful in pointing us in the right direction. Thank you!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms