# NullReferenceException in my inventory system [duplicate]

I'm making a simplified inventory system for a 3d game.

When I try to pick up an object, I get the following error:

NullReferenceException: Object reference not set to an instance of an object

Here are the scripts I'm using:

Pick Up goes on the item - the error fires inside the Update method

public class PickUp : MonoBehaviour {
private playerInventory inventory;
public Transform woodIcon;
public Transform slot1;
public void Start()
{
inventory = GameObject.FindGameObjectWithTag("Player").GetComponent<playerInventory>();
}

public void Update()
{
if (inventory.slot1 == true) // <-- error fires on this line.
{
Destroy(gameObject);
Instantiate(woodIcon, slot1.position, woodIcon.rotation);
}
}
}


Player Inventory

public class playerInventory : MonoBehaviour {

public bool slot1;

public void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.name == "wood")
{

}
if (slot1 == false)
{
slot1 = true;
}
}
}


Here is a screen shot showing my setup and the error:

• You haven't actually said what the problem is; could you describe it a little please? Right now this question is at risk of being closed as it's not clear what you're asking. Describing the problem could remove that risk and help you get better answers. – Maximus Minimus Oct 29 '18 at 3:10
• If you open your console, the error gives you a line number as well, a variable you have seems to be null, but that doesn't really solve the "how to make an inventory system". – TomTsagk Oct 29 '18 at 9:57
• You will find the complete error message with the script filename and line number on the "Console" tab. – Philipp Oct 29 '18 at 10:12
• Don't forget to search for the text of your error as step one - you'll often find existing help about it. – DMGregory Oct 30 '18 at 15:03

You are trying to find the game object with the inventory with GameObject.FindGameObjectWithTag("Player"). However, the game object with the player inventory which you show on the screenshot has the Tag "Untagged".

That means FindGameObjectWithTag returns null. You then try to call .GetComponent on a null object, which throws the first NullReferenceException and crashes your Start method. The variable private playerInventory inventory; stays null, so you get further exceptions whenever you try to access inventory.slot1.

In order to fix this problem, change the tag of the player game object to "Player".

There also seems to be something which isn't syntactically but logically weird about the interaction between these two scripts. Why do all wood items in the game self-destruct and instantiate an item as soon as some variable of the global player inventory changes? However, you are not describing properly what this code is actually supposed to be doing, so maybe that's what you want it to do.

• Rather than just fix the tag, I'd remove the tag check entirely. If what the user wants is to find the instance of the PlayerInventory type, they can do this directly with FindObjectOfType, or a singleton/locator pattern. This removes the assumption about where the inventory script is organized in the scene, and focuses on the functional result the rest of the code depends on (ie. an instance of this specific type). – DMGregory Oct 29 '18 at 11:50
• @Philipp thank you for the help sorry i wasn't more clear on what this did, i'm trying to brute force an inventory system with one slot, because i dont know how to use arrays. the "wood" gme object is destroyed when the player touches it and the i con is instantiated in the slot when the slot is set to full. – Indiepixel Oct 29 '18 at 14:40

excuse me if this is totally wrong but i would start by making an 'array' for the inventory slots. then i would make a very simple option(if/else chain) for each item like equip, un-equip, and drop.

like:

#include <stdio.h>

int main()
{
int inventory [9]; //int = data type, inventory = array name, 9 = size
return 0;
}


as you approach a struct(item) have a string(some words) appear on screen

printf("item axe 50 damage : ");


if you want to pickup said item then

scanf("%d",&controller_button_1);

if (controller port == 1)
printf("\nitem equiped is : %d ",item axe 50 damage);

• Please note that this question is tagged Unity, so C/C++ conventions like int main() printf and scanf are not necessarily appropriate here. – DMGregory Oct 29 '18 at 13:01