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 /
avatar image
0
Question by StugTheGuy · Oct 14, 2019 at 03:05 AM · c#arraynullreferenceexceptioninventory

Array items null but appear normal in inspector

Hi all, I've been using unity for a few years but I am really new to arrays. I'm sure there's a simple explanation to this, but I am getting a value of null when trying to access any of the items in InvItems after setting invItems to an array of new Items. Here is the code to the inventory class:

     public Item[] invItems;
 
     void Start()
     {
         invItems = new Item[8];
         print(invItems[0]);
     }

Here is the code for the Item class (in the same script):

 public string name;
 public int ID;

 public Item()
 {
     name = "nullItem";
     ID = -1;
 }

When I enter playmode, it prints out Null for any of the items in the array. But I can see that it actually creates the items in the inspector: alt text

Any advice or help is greatly appreciated!

snipunityinv.png (188.9 kB)
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

2 Replies

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

Answer by GrayLightGames · Oct 14, 2019 at 05:31 AM

Hi @StugTheGuy, I'd recommend using the List class instead of arrays, seems a bit more flexible and user friendly. However, my guess is that when you create an array with a size of 8, it creates an array with 8 slots but doesn't actually call your constructor and put items in those slots. The inspector itself is probably doing that to make sure it doesn't throw an exception. Not sure, but your error makes it seem like from your script's perspective, the array is size 8 and empty.

I would try setting Item[0] = new Item() before your print statement and see if that gets you past the error. Hope that helps!

Comment
Add comment · Show 4 · 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 StugTheGuy · Oct 14, 2019 at 02:48 PM 0
Share

thank you that makes sense, i'll use a for loop to set the items individually. but do you know why it wouldnt call the constructor when creating the list? is it because unity has no way to know which constructor to use?

avatar image GrayLightGames StugTheGuy · Oct 14, 2019 at 03:39 PM 1
Share

Honestly, not completely sure... when an array is created, it fills itself with default values. For ints, as an example, the default value is 0. For a class, it's null. It's just how C# and java does it... my best guess is it's the easiest, cheapest way to create the array and not every situation would call for the elements to be filled at creation time. You also made a good point, it's unknown which constructor to use. Or perhaps, it guarantees that the array can be created and doesn't error out on anyone's constructor code. Which means the internal array constructor has to do less error handling. You could certainly write your own function that would initialize an array to a size and then call a constructor for each slot if that would help you. I find most of the time with an array or a list though, you will probably want to set some values in each array element when you create it, so you would probably want to make a better constructor and wouldn't end up using the default null constructor very much anyway. Glad to help, please accept my answer if that solved it.

avatar image StugTheGuy GrayLightGames · Oct 14, 2019 at 04:01 PM 0
Share

That makes sense, I'm using my own function that initializes each Item individually now so it does work, but thank you so much for your explanation and help. Happily accepting your answer.

Show more comments
avatar image
1

Answer by HappiiGamer · Oct 14, 2019 at 03:05 PM

By default the invItems = new Item[8] creates an array of 8 nulls.

You need to initialize the items first. Loop through the array and set each item to new Item().

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 StugTheGuy · Oct 14, 2019 at 03:59 PM 0
Share

I ended up doing almost exactly like that, thank you for the advice :0

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

134 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 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 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

Can't Assign Item In Array 1 Answer

NullReferenceException - Maze Generator Script 1 Answer

Array of custom properties? (C#) 1 Answer

Unity event calling function gets nullreferencexception on bool 0 Answers

Need a hand using array.Length in a C# editor script 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