NullReferenceException Error!! Cant Fix It :(

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

public class AuctionManager : MonoBehaviour
{
public static AuctionManager Instance;
public List Items = new List();

public Transform ItemContent;
public GameObject InventoryItem;

private void Awake()
{
Instance = this;
}

public void Add(Item item)
{
Items.Add(item);
}

public void Remove(Item item)
{
Items.Remove(item);
}

public void ListItems()
{
foreach (var item in Items)
{
GameObject obj = Instantiate(InventoryItem, ItemContent);
//var itemName = obj.transform.Find(“Items/ItemName”).GetComponent();
var itemIcon = obj.transform.Find(“Items/ItemIcon”).GetComponent();

//itemName.text = item.itemName;
itemIcon.sprite = item.icon;
}
}
}

Pls Help

People are more likely to help when you put a little more effort in creating a good post.
Use “code”-tags, so your code shows up code-formatted instead of regular text.
Share the complete error message, it has valuable information.

I see you’re learning singletons, kudos! You’ve just stepped onto the battlefield, I hope you’re prepared for the life long battle. :slight_smile:

Obviously your problem is here:

public void ListItems()
    {
        foreach (var item in Items)
        {
            GameObject obj = Instantiate(InventoryItem, ItemContent);
            //var itemName = obj.transform.Find("Items/ItemName").GetComponent<Text>();
            var itemIcon = obj.transform.Find("Items/ItemIcon").GetComponent<Image>();

            //itemName.text = item.itemName;
            itemIcon.sprite = item.icon;
        }
    }

Your “Item” class should already have a gameObject associated with it, as well as any text or images. Which should be cached into that class. So any call to a particular “Item” just needs to reference that variable within that class. So technically that function should look like this:

public void ListItems()
    {
        foreach (var item in Items)
        {
            itemName.text = item.myText;
            itemIcon.sprite = item.myIcon;
        }
    }

I removed the gameObject.Instantiate() because I’m not sure exactly how that works within correlation, of what exactly it is you need to do. But once you have a class reference, you should be easily able to get anything in said class, whether it be the gameObject, Renderer, Collider, Rigidbody, or any declared variables like myIcon, myText, etc…

Always cache important, or needed, variables and have them as “public”. So all you need to do is this.variable = className.variable.

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: Using code tags properly

The answer is always the same for NullRef… ALWAYS!

How to fix a NullReferenceException error

https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

Three steps to success:

  • Identify what is null ← any other action taken before this step is WASTED TIME
  • Identify why it is null
  • Fix that

The error message will have two numbers in parentheses at the end. The first number is the line number where the error occurred, the second number is the column where the error occurred. Once you know that, then it’s pretty easy to identify which object is null.

2 Likes

THX