Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

same errors with different code

Discussion in 'Scripting' started by rayman30111, Jan 12, 2020.

  1. rayman30111

    rayman30111

    Joined:
    Jan 9, 2020
    Posts:
    3
    I copied some code I used from one game to another and some of it works correctly and the inventory/ui system i implemented keeps having the same error. This is the example I currently have with this error.

    NullReferenceException: Object reference not set to an instance of an object
    PlayerInteract.Awake () (at Assets/Scripts/PlayerInteract.cs:16)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerInteract : MonoBehaviour
    6. {
    7.     Collider2D item;
    8.     private Inventory inventory;
    9.     [SerializeField] private UI_Inventory uiInventory;
    10.  
    11.  
    12.     private void Awake()
    13.     {
    14.  
    15.         inventory = new Inventory();
    16.         uiInventory.SetInventory(inventory);
    17.  
    18.         ItemWorld.SpawnItemWorld(new Vector3(2.5f, 3.5f), new Item { itemType = Item.ItemType.Fireball, amount = 1 });
    19.         ItemWorld.SpawnItemWorld(new Vector3(2.0f, 3.5f), new Item { itemType = Item.ItemType.Potion, amount = 1 });
    20.  
    21.     }
    22.  
    23.     //private void Update()
    24.     //{
    25.     //    if (currentInterObj && Input.GetButtonDown("Interact"))
    26.     //    {
    27.     //        currentInterObj.SendMessage("DoInteraction");
    28.     //    }
    29.     //}
    30.  
    31.     private void OnTriggerEnter2D(Collider2D collider)
    32.     {
    33.         ItemWorld itemWorld = collider.GetComponent<ItemWorld>();
    34.         if (itemWorld != null)
    35.         {
    36.             inventory.AddItem(itemWorld.GetItem());
    37.             itemWorld.DestroySelf();
    38.         }
    39.     }
    40. }
    41.  
    ------------------------------------------------------------------------------------------------------------------------------------------------
    The Inventory code is:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using System;
    6.  
    7. public class Inventory
    8. {
    9.     public event EventHandler OnItemListChanged;
    10.  
    11.     private List<Item> itemList;
    12.  
    13.  
    14.     public Inventory()
    15.     {
    16.         itemList = new List<Item>();
    17.         //AddItem(new Item { itemType = Item.ItemType.Fireball, amount = 1 });
    18.         //AddItem(new Item { itemType = Item.ItemType.Potion, amount = 1 });
    19.         //AddItem(new Item { itemType = Item.ItemType.Fireball, amount = 1 });
    20.         //AddItem(new Item { itemType = Item.ItemType.Potion, amount = 1 });
    21.     }
    22.  
    23.     public void AddItem(Item item)
    24.     {
    25.         itemList.Add(item);
    26.         OnItemListChanged?.Invoke(this, EventArgs.Empty);
    27.     }
    28.  
    29.     public List<Item> GetItemList()
    30.     {
    31.         return itemList;
    32.     }
    33. }
    The original video that I took the code from is a Code Monkey video.
     
  2. Agent003

    Agent003

    Joined:
    Sep 7, 2018
    Posts:
    55
    I think you need to initialize your uiInventory
    uiInventory = new uiInventory();
     
    rayman30111 likes this.
  3. rayman30111

    rayman30111

    Joined:
    Jan 9, 2020
    Posts:
    3
    Thanks but that didn't do it :/
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,151
    Since line 16 is your error line, then @Agent003 is correct. uiInventory is null. You made it a SerializeField though, so either it's being overwritten in the inspector, which makes it null, or you forgot to drag something into that slot.

    If uiInventory is a monobehaviour, then you can't initialize it with new, you'd have to do some GetComponent or AddComponent type call, but I'm guessing since it's a SerializeField, you just need to drag the gameObject in your scene that has the script into the slot.

    Take a look at about 5:46 in the video to see what I'm talking about(after the youtuber creates the variable). Remember when following tutorials to follow it all the way or little details will be missed.
     
    rayman30111 likes this.
  5. rayman30111

    rayman30111

    Joined:
    Jan 9, 2020
    Posts:
    3
    So I see what you did there and yes I think I edited the code and it took the object out of the script. I put it back in and the error is gone. Thanks for that. Its giving me the same error for a different script. We could probably do this all day and there is no object that needs to be added to this script. BTW love the quick results and the support. Much appreciated. I am going to look through the video some more and see what I could possibly be doing wrong with all my scripts and make sure I didn't miss anything.
     
    Last edited: Jan 13, 2020