Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by hilariousjello · Jan 10, 2014 at 02:17 AM · nullreferenceexception

NullReferenceException

Trying to create a system where the InventoryHandler reads an array of the names of items in your inventory, sends them to an ItemList via method CompareItem(), and receives GameObjects of type Item in return, which it can then use to instantiate the items in the scene. I am, however, getting a NullReferenceException on the names of the items being sent. What am I missing?

public class InventoryHandler : MonoBehaviour {

 public string [,] inventory = new string[10, 4];
 public Transform place;  //Used for locating where to instantiate items
 public float spacing = 1;// Ditto
 ItemList itemlist = new ItemList();

 void Start () {
     for(int i = 0; i < inventory.GetLength(0); i++)
         for(int j = 0; j < inventory.GetLength(1); j ++){
                 inventory[i, j] = "it1";  // Fills inventory array with items.
     }
 }
 
 void Update () {
     for(int i = 0; i < inventory.GetLength(0); i++)
         for(int j = 0; j < inventory.GetLength(1); j ++)
         {
             string current = inventory[i, j];
             Item item = itemlist.CompareItem(current); //Exception thrown here.
             Instantiate(item, new Vector3(place.position.x + (spacing * i), place.position.y - (spacing * j)), Quaternion.identity);
         }
 }

public class ItemList : MonoBehaviour {

 public Item[] items;
 public Item empty;
 public Item it1;  //These are defined in the Inspector.
 public Item it2;

 // Use this for initialization
 void Start () {
     items = new Item[]{
         it1,
         it2
     //etc. etc.
     };
 }

 public Item CompareItem(string nameToCheck){
     for(int i = 0; i < items.GetLength(0); i++)
         if (items[0].itemName == nameToCheck){ //Same exception also thrown here.
             return items[0];
         }
     return null;    
 }

}

public class Item : MonoBehaviour {

 public string itemName;

}

public class Item1Inventory : Item { void Start(){ itemName = "it1"; } }

The error message is:

NullReferenceException: Object reference not set to an instance of an object ItemList.CompareItem (System.String nameToCheck) (at Assets/ItemList.cs:34) InventoryHandler.Update () (at Assets/InventoryHandler.cs:25)

Comment
Add comment · Show 2
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 robertbu · Jan 10, 2014 at 02:28 AM 0
Share

What specific line is giving you the null reference exception? In addition, can you edit your question and paste the error from the console into the question?

avatar image hilariousjello · Jan 10, 2014 at 03:21 AM 0
Share

That would probably help. :P

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Jan 10, 2014 at 04:33 AM

The issue is that you are deriving ItemList from Monobehaviour, but you are using the 'new' operator in InventoryHandler on line 4 to create the ItemList. You cannot use the 'new' operator to create something derived from Monobehavior. Monobehaviour-derived classes only exist as components of GameObjects. You could attach ItemList to a game object, but given your code here, I suggest that youchange both the 'Item' class and the 'ItemList' class so they are not derived from Monobehavior. To initialize ItemList, you will use a standard C# constructor. Just change 'void Start()' to 'ItemList()'.

Comment
Add comment · Show 1 · 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 hilariousjello · Jan 14, 2014 at 11:49 PM 0
Share

That did clear a yellow flag warning, but no luck on the NullReferenceException. Also, it seems I have to leave Item derived from $$anonymous$$onoBehavior, because otherwise Unity throws a fit about instantiating one of its children. :P

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 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

Related Questions

Problem with Null Reference Exeption??:/ 1 Answer

NullReferenceException: Object reference not set to an instance of an object 0 Answers

NullReferenceException 1 Answer

Null Reference Exception Hunt ! 1 Answer

Array of custom properties? (C#) 1 Answer


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