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!
