0

I've been following this tutorial series: https://www.youtube.com/watch?v=LcizwQ7ogGA&list=PLJWSdH2kAe_Ij7d7ZFR2NIW8QCJE74CyT&index=3 I'm on part 3 of 10.

Good series so far, however I've hit a very major problem. When I go over to pick an item up, it recreates the item, indefinately causing the enitre game to lag very quickly, Figure 1. I'm not sure what else to do as anything I do doesn't fix it at all. Is there anything anytone can spot that is wrong or that is causing this? The only error I get is that Line 48's object reference is not set to an instance of an object, Figure 2. This might be because I have a new prefab in the Inspector, Figure 3.

Here is the code for Display Inventory:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class DisplayInventory : MonoBehaviour
{
    public GameObject inventoryPrefab;

    public InventoryObject inventory;

    public int xStart;
    public int yStart;

    public int xSpaceBetweenItem;
    public int numberOfColumns;
    public int ySpaceBetweenItem;

    Dictionary<InventorySlot, GameObject> itemsDisplayed = new Dictionary<InventorySlot, GameObject>();

    // Start is called before the first frame update
    void Start()
    {
        CreateDisplay();
    }

    // Update is called once per frame
    void Update()
    {
        UpdateDisplay();
    }

    public void UpdateDisplay()
    {
        for (int i = 0; i < inventory.Container.Items.Count; i++)
        {
            InventorySlot slot = inventory.Container.Items[i];

            if (itemsDisplayed.ContainsKey(slot))
            {
                itemsDisplayed[slot].GetComponentInChildren<Text>().text = inventory.Container.Items[i].amount.ToString("n0");
            }
            else
            {
                var obj = Instantiate(inventoryPrefab, Vector3.zero, Quaternion.identity, transform);
                obj.transform.GetChild(0).GetComponentInChildren<Image>().sprite = inventory.database.GetItem[slot.item.id].uiDisplay;
                obj.GetComponent<RectTransform>().localPosition = GetPosition(i);
                obj.GetComponentInChildren<Text>().text = slot.amount.ToString("n0");
                itemsDisplayed.Add(inventory.Container.Items[i], obj);
            }
        }
    }

    public void CreateDisplay()
    {
        for (int i = 0; i < inventory.Container.Items.Count; i++)
        {
            InventorySlot slot = inventory.Container.Items[i];

            var obj = Instantiate(inventoryPrefab, Vector3.zero, Quaternion.identity, transform);
            obj.transform.GetChild(0).GetComponentInChildren<Image>().sprite = inventory.database.GetItem[slot.item.id].uiDisplay;
            obj.GetComponent<RectTransform>().localPosition = GetPosition(i);
            obj.GetComponentInChildren<Text>().text = slot.amount.ToString("n0");
            itemsDisplayed.Add(slot, obj);
        }
    }

    public Vector3 GetPosition(int i)
    {
        return new Vector3(xStart + (xSpaceBetweenItem * (i % numberOfColumns)), yStart + (-ySpaceBetweenItem * (i / numberOfColumns)), 0f);
    }
}
Somiaz
  • 1
  • 2
  • _"The only error I get is that Line 48's object reference is not set to an instance of an object"_ -- that's a really important error to fix. You cannot have correct code when that exception is thrown. See duplicate for advice on how to fix it. If you are still having trouble after dealing with that, then post a new question with better specifics, a good [mcve] and an explanation of what exactly you need help with. Things like "can anyone help me?", "can anyone spot what's wrong with this code?", etc. don't make for good questions here. – Peter Duniho Feb 22 '21 at 23:36
  • The things is, I have already set what it needs to instantiate in figure 2, but it keeps creating them. Are you sure that the error is what is causing all of this? – Somiaz Feb 23 '21 at 00:14
  • It's impossible to be sure of much of anything without a [mcve]. However, it is easy to be sure that until the `NullReferenceException` is diagnosed and fixed, that you cannot trust anything else about the program. Unity3d in particular is terrible about this, because it will swallow the NRE and keep going; most environments, the program will simply stop and you won't have a choice. But even in Unity3d, you _must_ fix the NRE before you go any further. Maybe that will solve the other issue, maybe it won't, but you can't do anything useful with the problem until the NRE is fixed. – Peter Duniho Feb 23 '21 at 00:19
  • Alright, I found it. It was looking for a text gameobject that didn't exist. I didn't realise I deleted it. Sorry for the trouble. – Somiaz Feb 23 '21 at 00:49

0 Answers0