• 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
1
Question by Oldskewl · Jan 03, 2014 at 02:05 AM · arrays

Array returning null

I have an array of game objects that I'm trying to search through to get a reference to the one named but I keep getting returned a null and I'm not sure why. I'm a beginner at programming so please temper your answer toward a beginner's understanding, many thanks.

 using System.Linq;
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class ItemManager : MonoBehaviour
 }
   public Item[] Items;
 
   void Start ()
 {
 for (int i = 0; i < Items.Length; i++)
     {
       Debug.Log ("Item Name: " + Items[i].Name);
     }
 }
 
 //gets a reference to the item with this name
         public Item GetItemByName(string itemName)
         {
             Debug.Log ("inside GetItemByName looking for: " + itemName);
             return Items.FirstOrDefault(item => item.Name == itemName);
         }
 }

GetItemsByName () is called by another script when the object the other script is attached to is destroyed using OnDestroy ().

Comment
Add comment · Show 3
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 Crucian · Jan 03, 2014 at 03:06 AM 0
Share

Are you getting a Null reference exception error? If so you probably need to assign some Items into your Item array.

avatar image Oldskewl · Jan 03, 2014 at 06:08 AM 0
Share

there would be a Null reference exception but the function that calls this one checks for null. I have a few debug checks and all but this one come back with the right item name.

avatar image Imankit · Jan 03, 2014 at 06:29 AM 0
Share

You need to give a instance to your items array..

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by ncallaway · Jan 03, 2014 at 06:48 AM

Unless you have other code initializing the Items array it will always be null. This would cause a NullPointerException (NPE) in Start() on line 14 and in GetItemByName() on line 22.

You could fix this by initializing Items to some default starting size:

 public class ItemManager : MonoBehaviour
 {
   public Item[] Items = new Item[5]
   ...

If you're using Items as a variable size container I might recommend using System.Collections.Generic.List (http://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx) instead.

Your code would change to something like

 using System.Linq;
 using UnityEngine;
 using System.Collections.Generic;
  
 public class ItemManager : MonoBehaviour
 {
     // the main difference from using an array is the declaration and initialization
     // of Items here.
     public List<Item> Items = new List<Item>();
  
     void Start ()
     {
         for (int i = 0; i < Items.Count; i++)
         {
             Debug.Log ("Item Name: " + Items[i].Name);
         }
     }
  
     // gets a reference to the item with this name
     public Item GetItemByName(string itemName)
     {
         Debug.Log ("inside GetItemByName looking for: " + itemName);
         return Items.FirstOrDefault(item => item.Name == itemName);
     }
 }
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 Oldskewl · Jan 03, 2014 at 09:02 PM 0
Share

looks like I had a typo in one of the item names that was assigned in the inspector, found it by using a bunch of debug statements.

I'm also going to look at using a list ins$$anonymous$$d of an array, thank you for the help.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

How to select a texture in an inventory? 0 Answers

How to load an inventory array? 1 Answer

hi!does any know how to make an endless track 1 Answer

Get ParticleSystem (Shuriken) to play from array of game objects C# 2 Answers

How to find the index of an Object in an Array 2 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