1

I am using unity

I am trying to destroy a gameObject in Unity, and once that gameObject is destroyed, It adds the item to the player's inventory. The issue is, when I call this method inside Unity, I get the error:

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

all of the gameObject destruction is handled in my Character script under this method:

    void PlayerInput()
    {
        if (Input.GetKeyDown(KeyCode.Mouse0))
        {
            Vector3 c = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            RaycastHit2D hit2D = Physics2D.Raycast(transform.position, c - transform.position);

            if (hit2D.collider.gameObject != null)
            {
                int TileID = hit2D.collider.gameObject.GetComponent<IDScript>().ObjectID;
                //inventory.AddItem(TileID);
                Destroy(hit2D.collider.gameObject);
            }
        }
    }

my issue arises once I remove the comment from inventory.AddItem(TileID); and try to run it. here is the method AddItem:

    public void AddItem(int id)
    {
        Item itemToAdd = database.FetchItemByID(id);

        for (int i = 0; i < Items.Count; i++)
        {
            if (Items[i].ID == -1)
            {
                Items[i] = itemToAdd;
                GameObject itemObj = Instantiate(inventoryItem);
                itemObj.transform.SetParent(slots[i].transform);
                itemObj.transform.position = Vector2.zero;
                itemObj.GetComponent<Image>().sprite = itemToAdd.Sprite;
                itemObj.name = itemToAdd.Title;

                break;
            }
        }
    }

I did a little bit of debugging, and found that when I run Debug.Log(Inventory); in my Character class, the Inventory class returns null. here is the code I use to "introduce" my Inventory class to the character class:

Inventory inventory;

    void Start()
    {
        inventory = GetComponent<Inventory>();
    }

Again, I am horribly sorry if this is an easily fixable issue or something that can be found with a search query. I have searched for a good while and nothing seems to be helpful. I'm probably doing something wrong, but if I need to provide the full code I am more than willing. sorry for the long question. Thanks!

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
  • Don't know why this was marked as a duplicate, the issue here isn't with the NRE being thrown, but rather with why does GetComponent() returns null. Which, if I were to guess, I'd avance that it's because your gameObject does not have an Inventory component? – Etienne de Martel Aug 16 '18 at 00:29
  • The Inventory Component is attatched to a gameobject called "Inventory." Is this done incorrectly? – Vinsanityjr Aug 16 '18 at 00:34
  • And the script that does the GetComponent(), is it on that same gameObject? Because GetComponent() only looks up components on the same gameObject. – Etienne de Martel Aug 16 '18 at 00:36
  • oh, no! thank you for helping me out here. the GetComponent() script is on my player. is there a different way that I need to call this? – Vinsanityjr Aug 16 '18 at 00:37
  • Instead of using GetComponent() to initialize the field, make it public, and then it'll show up in the inspector for your player. You can then drag/drop the inventory gameObject on that to create a reference. – Etienne de Martel Aug 16 '18 at 00:39
  • @Etienne de Martel I agree on the duplicate. You should provide an answer here. – Jimi Aug 16 '18 at 01:16
  • @Jimi I would, but the question needs to be reopened first. – Etienne de Martel Aug 16 '18 at 15:45
  • @Etienne de Martel I voted for reopeing. Ping Plutonix and Camilo Terevinto. – Jimi Aug 16 '18 at 15:48

0 Answers0