The question is answered, right answer was accepted
NullReferenceException: Object reference not set to an instance of an object
Hey, Im working on a inventory-system and I keep getting this error:
NullReferenceExeption: Object reference not set to an instance of an object Inventory.Start() (at Assets/.../Inventory.cs:32)
I have no idea what causes it because Im pretty new to Unity and C#. I've already read some other Posts but I didn't find anything helpful.
Heres the code:
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Inventory : MonoBehaviour {
 
     public GameObject slotPrefab;
     private List<GameObject> allSlots;
 
     public GameObject Slot1, Slot2, Slot3, Slot4, Slot5, Slot6, Slot7, Slot8, Slot9, Slot10 ,Slot11 ,Slot12 ,Slot13 ,Slot14;
 
     private int emptySlots;
     void Start ()
     {
         allSlots.Add(Slot1);
         allSlots.Add(Slot2);
         allSlots.Add(Slot3);
         allSlots.Add(Slot4);
         allSlots.Add(Slot5);
         allSlots.Add(Slot6);
         allSlots.Add(Slot7);
         allSlots.Add(Slot8);
         allSlots.Add(Slot9);
         allSlots.Add(Slot10);
         allSlots.Add(Slot11);
         allSlots.Add(Slot12);
         allSlots.Add(Slot13);
         allSlots.Add(Slot14);
 
         emptySlots = 14;
     }
 
     void Update () 
     {
     
     }
 
     public bool AddItem(Item item)
     {
         if(item.maxStack == 1)
         {
             PlaceInEmpty(item);
             return true;
         }
         return false;
     }
 
     private bool PlaceInEmpty(Item item)
     {
         if(emptySlots > 0)
         {
             foreach (GameObject slot in allSlots)
             {
                 Slot tmp = slot.GetComponent<Slot>();
 
                 if(tmp.IsEmpty)
                 {
                     tmp.AddItem(item);
                     emptySlots--;
                     return true;
                 }
             }
         }
 
         return false;
     }
 }
Answer by high500 · Mar 03, 2016 at 07:12 PM
Perhaps each time you create an instance of your class object you need to instantiate a new list
 void Start ()
      {
          allSlots = new List<GameObject>;
          allSlots.Add(Slot1);
          allSlots.Add(Slot2);
          allSlots.Add(Slot3); ....
Its late and just finished work so I maybe off a little lol!
Give it a try, the error is basically telling you that an object hasn't been properly referenced/created, Is that line number 32 where the error occurs? if so its possible its the "emptySlots = 14;"
Therefore try:
 this.emptySlots = 14;
I maybe way off sorry if I am!
Stewart
No problem. Please can you be clear which part it was please? Incase anyone else gets the same issue. Was it the new List in guessing?
Cheers
I basicly did what you said. I added "allSlots = new List ();" in Start and replaced "emptySlots = 14" with "this.emptySlots = 14;".
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                