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

Hi Everyone!

For the last 2 days I worked my brain soo hard to resolve this problem but I couldn’t…
I’m quite new to game developing, I worked at my game for 1 month and I want to make a shopping system: the system is not very complicated: I make a UI design and after that I add a “ShopItem”, this should be the container for the skins you want to buy for the player, I made a script for “shop” to generate multiple list elements and a scroll mechanic, all goes good until I tried to run the game, I got this “NullReferenceException: Object reference not set to an instance of an object”, and I don’t understand how to resolve it…
this is the prefab for “ShopItem”:

And this is the error I get every time I try to run the game:

The problem is I can’t make 2 elements or more in the list because i got the error and the prefab isn’t updateing proprely…

Here are the code I used for Shop system:
using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;

public class Shop : MonoBehaviour
{
[System.Serializable] class ShopItem
{
public Sprite Image;
public int Price;
public bool IsPurchased;
}

[SerializeField] List ShopItemsList;

GameObject ItemTemplate;
GameObject g;
[SerializeField] Transform ShopScrollView;
Button buyBtn;

private void Start()
{
ItemTemplate = ShopScrollView.GetChild(0).gameObject;

int len = ShopItemsList.Count;
for (int i = 0; i < len; i++)
{
g = Instantiate(ItemTemplate, ShopScrollView);
g.transform.GetChild(0).GetComponent().sprite = ShopItemsList*.Image;*
g.transform.GetChild(1).GetChild(0).GetComponent().text = ShopItemsList*.Price.ToString();*
buyBtn = g.transform.GetChild(2).GetComponent();
buyBtn.interactable = !ShopItemsList*.IsPurchased;*
buyBtn.AddEventListener(i, OnShopItemButtonClicked);
}
Destroy(ItemTemplate);
}
void OnShopItemButtonClicked(int itemIndex)
{
Debug.Log(itemIndex);
}
}
If anyone knows what I should do I would be very gratefull, it’s my first game and I really love the idea of making makes… THANKS A LOT FOR EVERY EFFORT!

Please use code tags. I’m not gonna count 30 lines to tell you what the problem is, but whatever object reference(s) there are in line 30 of your Shop script, any one of those is null. Find out which one it is. Find out why it is null. Fix it. Nullreference exceptions are super common and the procedure to fixing them is always the same :slight_smile:

It is always the same… ALWAYS. It is the single most common error ever. Learn how to fix it fast!

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.