0
\$\begingroup\$

This question already has an answer here:

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:

error in the pick up script

\$\endgroup\$

marked as duplicate by congusbongus, DMGregory unity Oct 30 '18 at 15:01

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – Maximus Minimus Oct 29 '18 at 3:10
  • \$\begingroup\$ 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". \$\endgroup\$ – TomTsagk Oct 29 '18 at 9:57
  • \$\begingroup\$ You will find the complete error message with the script filename and line number on the "Console" tab. \$\endgroup\$ – Philipp Oct 29 '18 at 10:12
  • \$\begingroup\$ Don't forget to search for the text of your error as step one - you'll often find existing help about it. \$\endgroup\$ – DMGregory Oct 30 '18 at 15:03
0
\$\begingroup\$

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.

\$\endgroup\$
  • 1
    \$\begingroup\$ 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). \$\endgroup\$ – DMGregory Oct 29 '18 at 11:50
  • \$\begingroup\$ @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. \$\endgroup\$ – Indiepixel Oct 29 '18 at 14:40
0
\$\begingroup\$

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);
\$\endgroup\$
  • \$\begingroup\$ Please note that this question is tagged Unity, so C/C++ conventions like int main() printf and scanf are not necessarily appropriate here. \$\endgroup\$ – DMGregory Oct 29 '18 at 13:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.