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”.