Unable to update inventory slots

I am facing this error:
NullReferenceException: Object reference not set to an instance of an object
GameDevTV.UI.Inventories.InventoryUI.Awake () (at Assets/GameDev.tv Assets/Scripts/UI/Inventories/InventoryUI.cs:25)

I am using this asset outside of the coure’s git commit, and I am facing the above error. I have attached all the scripts and referenced inventory slots. I am unable to update the invenotry slots from the player UI script

1 Like

Can you paste the InventoryUI script using code formatting and any relevant screenshots to help me and other solve your problem.

Code formatting is when you paste your code and then format it using the </> icon, you highlight your code and then hit the </> button which is at the top where you are replying, and if you cant see it you need to hit the three lines drop-down

While the line numbers aren’t matching my copy of the InventoryUI script, this is the Awake() from the course. There is actually only one place that this particular error could be assuming that the Awake() method is unchanged.

        private void Awake()
            playerInventory = Inventory.GetPlayerInventory();
            playerInventory.inventoryUpdated += Redraw; //This is likely your error line

So awake has 2 lines. The first line is

playerInventory = Inventory.GetPlayerInventory();

If the error was here, then the reported error would actually have been in Inventory.cs. Here’s Inventory.GetPlayerInventory()

        public static Inventory GetPlayerInventory()
            var player = GameObject.FindWithTag("Player");
            return player.GetComponent<Inventory>();

This method looks for the player by searching for a GameObject with the tag “Player”. Once it finds that player, it calls GetComponent on that GameObject to get the player’s inventory.
If there was no GameObject with a “Player” tag, then this return statement would balk, because player would be null. That, however, is not the case. If the Player doesn’t have an Inventory on it, then null is returned.
This means that the null reference error is on the line

playerInventory.inventoryUpdated +=Redraw;

Now at this point, if the Player doesn’t have an Inventory component on it (or StatsInventory), then you’ll get a null reference error because we’re trying to call a method on a null reference.

This leaves two possibilities. Either the Player doesn’t have an Inventory (or StatsInventory), or there is another GameObject in the scene that also has the tag “Player” which is getting found before the Player is found in GameObject.FindWithTag(“Player”);

If your player has an Inventory (or StatsInventory) component, then you’ll need to go through each GameObject in your scene to find one tagged with “Player”.

1 Like

Thank you I had 3 game objects with the tag “Player” the issue is fixed :slight_smile: Unka is a dragon, Humanoid is human

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

Privacy & Terms