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 $$anonymous$$ · Dec 14, 2012 at 11:22 AM · c#listupdatenullreferencesystem.collections.generic

NullReference when updating a List

I have a Nullreference error when updating a List. This List only keep items for an inventory, but the problem is not the List itself but when it tries to update it the first time.

The List is in a separate menager using a singleton, but I'm trying to update it on the inventory buttons, the buttons when the inventory is activated will load the corresponding item of the list to the button number. I'm using NGUI to build up the inventory, but here is the button script:

 using UnityEngine;
 
 [AddComponentMenu("UI Scripts/PDA/Inventory/UI Inventory Item Button")]
 public class UIInvItemButton : UIInventoryButton {
     
     private bool isEmpty = true;
     
     public int itemSlotNumber;
     
     InventoryMenager inventoryMenager;
     ItemInfo itemInfo;
     
     UISprite UIItemPreviewSprite;
     UILabel UIInvItemDescLabel;
     UILabel UIInvItemDescTitleLabel;
     
     void Awake () {
         UIInvItemDescLabel = GameObject.Find ("/UIPlayer/Camera/UIAnchor_C/PDAPanel_C/InventoryPanel_C/UIInvItemDescArea/UIInvItemDescLabel").GetComponent<UILabel>();
         UIInvItemDescTitleLabel = GameObject.Find ("/UIPlayer/Camera/UIAnchor_C/PDAPanel_C/InventoryPanel_C/UIInvItemDescArea/UIInvItemDescTitleLabel").GetComponent<UILabel>();
 
     }
     
     void Start () {
         inventoryMenager = InventoryMenager.Instance;
     }
 
     void OnEnable () {
         UIInvItemDescLabel.enabled = false;
         UIInvItemDescTitleLabel.enabled = false;
     }
     
     //Check for items avaible in slots
     public void LoadInventoryItems() {
         UIItemPreviewSprite = GetComponentInChildren<UISprite>();
         
         if(isEmpty){
             if(inventoryMenager.itemsList.size > itemSlotNumber){
                 itemInfo = inventoryMenager.itemsList[itemSlotNumber];
             }
         }
 
         UIItemPreviewSprite.spriteName = (itemInfo != null) ? itemInfo.UIItemIconSelect : "inventoryItemSlotEmpty";
         isEmpty = (itemInfo != null) ? false : true;
     }
     
     //Overlay item description
     void OnClick () { 
         
         UIInvItemDescLabel.enabled = true;
         UIInvItemDescTitleLabel.enabled = true;
         
         if (!isEmpty) {
             UIInvItemDescLabel.text = itemInfo.itemDesc;
             UIInvItemDescTitleLabel.text = itemInfo.itemName;
         }
     }
 }
 

The function that is run is LoadInventoryItems(), it is run by another script that is the UI menager. I'm getting the error on this line:

if(inventoryMenager.itemsList.size > itemSlotNumber){

itemSlotNumber is manually written on the inspector starting from 0 as int. I can avoid this problem only by putting that function in Update, but I want to avoid it as updating a List on 16 buttons can be considerably a performance loss.

The eror only appear once, then as soon I pickup an item is all ok and it update. Any help is apreciate.

On a side note, you can notice that the inventorymenager uses size instead of Count, that because I've been using BetterList of NGUI, but is the same thing.

Comment
Add comment
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

4 Replies

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

Answer by Nidre · Dec 14, 2012 at 01:40 PM

As i understood you don't get any null referance exception when you put the LoadInventoryItems method on Update method.And when you put it out of Update this exception is only appearing once.

If i understood correctly that might mean that

 void Start () {
 inventoryMenager = InventoryMenager.Instance;
 }

did not fired yet when you are calling LoadInventoryItems method first time.

Can you replace your LoadInventoryItems method with the following method to see if thats the case.

     //Check for items avaible in slots
     public void LoadInventoryItems() {
        UIItemPreviewSprite = GetComponentInChildren<UISprite>();
 
        if(isEmpty){
          if(InventoryMenager.Instance.itemsList.size > itemSlotNumber){
           itemInfo = InventoryMenager.Instance.itemsList[itemSlotNumber];
          }
        }
 
        UIItemPreviewSprite.spriteName = (itemInfo != null) ? itemInfo.UIItemIconSelect : "inventoryItemSlotEmpty";
        isEmpty = (itemInfo != null) ? false : true;
     }

If the code above works for you.That means you are calling LoadInventoryItems before inventoryMenager is initialized.

You can try to replace your Start method with Awake method as a fix.

Hope this helps you out.

Comment
Add comment · 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
1

Answer by Shrandis · Dec 14, 2012 at 01:00 PM

 if (inventoryManager.itemsList.size > itemSlotNumber)

If this line is throwing a null reference exception, it can mean 2 things:

1)inventoryManager is null.

2)inventoryManager.itemsList is null.

There's also this code:

 void Start () {
        inventoryMenager = InventoryMenager.Instance;
     }

My guess is, LoadInventoryItems method is being called BEFORE this script's Start method assings inventoryManager, causing a null reference exception. Check where you call LoadInventoryItems and make sure inventoryManager is assigned to InventoryManager.Instance BEFORE you call LoadInventoryItems.

Comment
Add comment · 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
1

Answer by MarkFinn · Dec 14, 2012 at 01:13 PM

Almost certainly the issue is that the itemsList List object has not been created when this call is made.

The two options for a fix are :

1) Quick and dirty (add the extra check):

if (inventoryMenager.itemsList==null){
      inventoryMenager.itemsList=new List();
}
if(inventoryMenager.itemsList.size > itemSlotNumber){

2) Proper interface based design:

Make the itemsList List private within the singleton, and add public methods Size(), Add(Whatever newitem), etc... which cope properly with the list being null or empty.

Comment
Add comment · 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
0

Answer by $$anonymous$$ · Dec 14, 2012 at 09:52 PM

All of you were right, the instance weren't getting there in time, still putting it on Awake didn't help like it got ignored, but I did a work around putting a public function only for the instance:

public void InstantiateInventory () { inventoryMenager = InventoryMenager.Instance; }

Then I just call that on the my Ui menager at startup, after that it all goes right. I guess the main problem was the fact that the UIInvItemsButton is deactivated at startup and so the code just skip it. By the way thanks to everyone for the help.

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 Nidre · Dec 14, 2012 at 09:59 PM 0
Share

Glad that i helped you :)

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

11 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

Related Questions

A node in a childnode? 1 Answer

Object Method returning null when calling Object List from another Class 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

cannot make a list with the word hello in it 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