Active Inventory null reference exception not fixed

The active inventory issue featured and fixed in this section of the 2D rpg combat course was not fixed for me and I am unsure why.

active inventory script below:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ActiveInventory : MonoBehaviour
{
   private int activeSlotIndexNum = 0;
   private PlayerControls playerControls;

   private void Awake(){
        playerControls = new PlayerControls();
   }

   private void Start() {
        playerControls.Inventory.Keyboard.performed += ctx => ToggleActiveSlot((int)ctx.ReadValue<float>());
        ToggleActiveHighlight(0);
   }

   private void OnEnable() {
        playerControls.Enable();
   }

   private void ToggleActiveSlot(int numValue){
     ToggleActiveHighlight(numValue-1);
   }

   private void ToggleActiveHighlight(int indexNum){
     activeSlotIndexNum = indexNum;
     foreach (Transform inventorySlot in this.transform) {
        inventorySlot.GetChild(0).gameObject.SetActive(false);
     }

     this.transform.GetChild(indexNum).GetChild(0).gameObject.SetActive(true);
   this.transform.GetChild(indexNum).GetChild(0).gameObject.SetActive(true);
    ChangeActiveWeapon();
   }

   private void ChangeActiveWeapon (){
    if (ActiveWeapon.Instance.CurrentActiveWeapon != null){
        Destroy(ActiveWeapon.Instance.CurrentActiveWeapon.gameObject);
    }
     
     Transform childTransform = transform.GetChild(activeSlotIndexNum);
    InventorySlot inventorySlot = childTransform.GetComponent<InventorySlot>();
    WeaponInfo weaponInfo = inventorySlot.GetWeaponInfo();
    GameObject weaponToSpawn = weaponInfo.weaponPrefab;
   
    if (weaponInfo==null){
        ActiveWeapon.Instance.WeaponNull();
        return;
    }
   
   GameObject newWeapon = Instantiate(weaponToSpawn, ActiveWeapon.Instance.transform.position, Quaternion.identity);
   ActiveWeapon.Instance.transform.rotation = Quaternion.Euler(0,0,0);
   newWeapon.transform.parent = ActiveWeapon.Instance.transform;
   ActiveWeapon.Instance.NewWeapon(newWeapon.GetComponent<MonoBehaviour>());
}

}

active inventory issue 1

There’s a LOT in ChangeActiveWeapon, and it’s next to impossible to determine which actual statement is the culprit in this…

Double click on the error, and a specific line in ChangeActiveWeapon will be selected in VS Code. That should help narrow things down, if you can mark the specific line.

I’ve attached a guide to pasting in code… simply pasting it in as text with no modifiers tends to make it difficult to format later.

1 Like

Hi thanks for the guide. I dont really understand it though, is there a video tutorial available to show me what’s required??

The issue says line 53.

Ok, that makes sense. What’s happening is that you’re using weaponInfo before it has been null checked. Move the line

GameObject weaponToSpawn = weaponInfo.weaponPrefab;

to after the null check clause

    if (weaponInfo==null){
        ActiveWeapon.Instance.WeaponNull();
        return;
    }
   GameObject weaponToSpawn = weaponInfo.weaponPrefab;
   GameObject newWeapon = Instantiate(weaponToSpawn, ActiveWeapon.Instance.transform.position, Quaternion.identity);

Heres’ the skinny on code formatting:
Next to the 1 key on your keyboard is a backwards quote symbol. It looks like this `, and not ‘.
On it’s own line before pasting in a block of code, type that symbol three times, it will look like this
```
not like
‘’’
Then you can paste or type in your code, for example
```

public void MyMethod()
{
    DoSomething();
}

```
becomes

public void MyMethod();
{
    DoSomething();
}
1 Like

Thanks dude.

Cheers for the advice on moving the code. So moving it the exact way you said caused another issues where it says I am calling the code before its defined. So I tried this (but I am not advanced enough to know if this will cause some other issue down the line or not:

Transform childTransform = transform.GetChild(activeSlotIndexNum);
     InventorySlot inventorySlot = childTransform.GetComponent<InventorySlot>();
     WeaponInfo weaponInfo = inventorySlot.GetWeaponInfo();
    if (weaponInfo==null){
        ActiveWeapon.Instance.WeaponNull();
        return;
    }
     
    GameObject weaponToSpawn = weaponInfo.weaponPrefab;

Good job! No, this code should work perfectly.

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

Privacy & Terms