Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Mar 11, 2017 at 10:06 PM by smeyr for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by smeyr · Mar 03, 2016 at 06:40 PM · errorinventoryinventory systemnull reference exception

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;
     }
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Graphics_Dev · Mar 03, 2016 at 06:53 PM 0
Share

Have you seen this?

http://answers.unity3d.com/questions/47830/what-is-a-null-reference-exception-in-unity.html

1 Reply

  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image smeyr · Mar 03, 2016 at 07:32 PM 0
Share

Thank you! Now it works perfectly.

avatar image high500 smeyr · Mar 03, 2016 at 07:40 PM 0
Share

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

avatar image smeyr · Mar 03, 2016 at 08:01 PM 0
Share

I basicly did what you said. I added "allSlots = new List ();" in Start and replaced "emptySlots = 14" with "this.emptySlots = 14;".

Follow this Question

Answers Answers and Comments

54 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Index was out of range. error 1 Answer

Concept of inventory system 0 Answers

OnTriggerEnter called second time with rigidbody 1 Answer

Is it possible to do Vuforia AR (pattern-based recognition) on top of Mapbox AR (location-based spawns)? 0 Answers

Null Reference in UnityStandardAssets.Utility.WaypointProgressTracker.Update 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges