so I have this code that should spawn random item when chest is opened but there is an error on itemChances = items*.chance line and i can’t change itemChances[] values (items are not null because debug.log on previous line works)*
Error: NullReferenceException: Object reference not set to an instance of an object
Chest.Start () (at Assets/Chest.cs:25)
code:
public class ItemsInChest
{
public GameObject item = null;
public int chance = 0;
}
public class Chest : MonoBehaviour
{
[SerializeField] private ItemsInChest[] items;
[SerializeField] private Transform itemPos;
private int[] itemChances;
void Start()
{
for (int i = 0; i < items.Length; i++)
{
Debug.Log(items_.item + " " + items*.chance);_
itemChances _= items.chance;_
_Debug.Log(itemChances);
}
}
public void OpenChest()
{
GameObject item = Instantiate(items[GetComponent().GetRandomOutcome(itemChances)].item, itemPos.position, Quaternion.identity);
item.transform.parent = itemPos;
GetComponent().SetBool(“Opened”, true);
}
}*_
ok i think i figured it out. I changed arrays into lists and it works