NullReferenceException: Object reference not set to an instance of an object (826036)

Full error:

NullReferenceException: Object reference not set to an instance of an object
LoadGameData.LoadData () (at Assets/Skrypt/LoadGameData.cs:54)
LoadGameData.Start () (at Assets/Skrypt/LoadGameData.cs:19)

Code here:

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

public class LoadGameData : MonoBehaviour
{


    public TextAsset GameData;
    public GameObject StorePrefab;
    public GameObject StorePanel;

    public void Start()
    {


        LoadData();
    }

    public void LoadData()
    {

        XmlDocument xmlDoc = new XmlDocument();

        xmlDoc.LoadXml(GameData.text);

        XmlNodeList StoreList = xmlDoc.GetElementsByTagName("store");

        foreach (XmlNode StoreInfo in StoreList)
        {

            GameObject NewStore = (GameObject)Instantiate(StorePrefab);

            store storeobj = NewStore.GetComponent<store>();



            XmlNodeList StoreNodes = StoreInfo.ChildNodes;
            foreach (XmlNode StoreNode in StoreNodes)
            {
                if (StoreNode.Name == "name")
                {
                   
                    Text StoreText = NewStore.transform.Find("StoreNameText").GetComponent<Text>();
                    StoreText.text = StoreNode.InnerText;
                }


                if (StoreNode.Name == "image")
                {
                    Sprite newSprite = Resources.Load<Sprite>(StoreNode.InnerText);
                    Image StoreImage = NewStore.transform.Find("ImageButtonClick").GetComponent<Image>();
                    StoreImage.sprite = newSprite;


                }

                if (StoreNode.Name == "BaseStoreProfit")
                    storeobj.BaseStoreProfit = float.Parse(StoreNode.InnerText);
                if (StoreNode.Name == "BaseStoreCost")
                    storeobj.BaseStoreCost = float.Parse(StoreNode.InnerText);
                if (StoreNode.Name == "StoreTimer")
                    storeobj.StoreTimer = float.Parse(StoreNode.InnerText);
            }
            NewStore.transform.SetParent(StorePanel.transform);
        }

    }
}

GameData code:

<gamedata>
  <store>
        <name>Zardzewiałe Akwarium</name>
        <BaseStoreCost>2</BaseStoreCost>
    <BaseStoreProfit>0,75</BaseStoreProfit>
    <StoreTimer>3</StoreTimer>
    <image>wood</image>
  </store>
  <store>
      <name>Skórzane Akwarium</name>
      <BaseStoreCost>10</BaseStoreCost>
    <BaseStoreProfit>3</BaseStoreProfit>
    <StoreTimer>6</StoreTimer>
    <image>leather</image>
  </store>
</gamedata>

this line is your error:

Image StoreImage NewStore.transform.Find("ImageButtonClick").GetComponent<Image>()

either it cannot find the GameObject or the Component :slight_smile:

The answer is always the same… ALWAYS. It is the single most common error ever. Don’t waste your life on this problem. Instead, learn how to fix it fast… it’s EASY!!

Some notes on how to fix a NullReferenceException error in Unity3D

  • also known as: Unassigned Reference Exception
  • also known as: Missing Reference Exception

http://plbm.com/?p=221

The basic steps outlined above are:

  • Identify what is null
  • Identify why it is null
  • Fix that.

Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.

This is the kind of mindset and thinking process you need to bring to this problem:

Step by step, break it down, find the problem.

1 Like