I had to do it a little differently to clear the error. Just an fyi

I had to check if the weaponInfo was null before trying to set the weaponToSpawn otherwise the error wouldnt clear.

private void ChangeActiveWeapon()
{
Transform childTransform = transform.GetChild(activeSlotIndexNum);
InventorySlot inventorySlot = childTransform.GetComponentInChildren();
WeaponInfo weaponInfo = inventorySlot.GetWeaponInfo();

    if(weaponInfo == null)
    {
        Debug.Log("WeaponInfo is null");
        ActiveWeapon.Instance.WeaponNull();
        return;
    }

    GameObject weaponToSpawn = weaponInfo.weaponPrefab;
4 Likes

Yes, null checking is essential, especially here.

My approach was to void the change from the start if there was no Weaponinfo

void ToggleActiveHighlight(int indexNum)
        {
            int oldIndex = activeSlotIndexNum;   
            activeSlotIndexNum = indexNum;
            if (activeSlot == null || activeSlot.GetWeaponInfo()==null)
            {
                activeSlotIndexNum = oldIndex;
                return;
            }
            //Change the active UI graphic
            //Change the ActiveWeapon

Then the player never winds up with no weapon if accidentally hitting the key for a blank weapon instead of the one they wanted.

2 Likes

Privacy & Terms