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 Cherno · Sep 23, 2013 at 05:02 PM · arraysinventory

2d array: finding out if an item is null?!?

My inventory script is structured as follows: A 2d array of GameObjects

 var InvMain : GameObject[ , ] = new GameObject[12,4]; 

first integer describes the inventory slot (12 total) and the second int describes the items in that slot, so stacked items can be stored. So far, so good.

Now, to display my stored inventory items, I use the simple code

 function OnGUI()
 {
      for (var x = 0; x < InvMain.Length; x ++)
      {
           if(InvMain[x,0] != null)//always show the first item of a stack of items
           {
                 //icon positions and sizes are stored in seperate lists that are not important for this question
                 //the coordinates here are just a placeholder
                GUI.DrawTexture (Rect(100 + x * 20, 100 + x * 20, 20, 20), InvMain[x,0].GetComponent(EL_Item).Icon);
           }
      }
 }


The problem here is the if(InvMain[x,0] != null) part, if my 2d array's second dimension is only size 1 (GameObject[12,1]), then it all works fine. If it's bigger than 1 however, then I get an "ArgumentOutOfRangeException: Argument is out of range." error.

It seems as even though I specifically check if the item at position [x,1], [x,2] and so on are not null, it still tries to access it and draw icon graphics etc.

How does one check if an item at a specific positions inside a 2d array is null? Also, my 2d array doesn't show up in the inspector :(

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Cherno · Sep 23, 2013 at 07:36 PM

I found out what the issue here was myself:

this code:

 for (var x = 0; x < InvMain.Length; x ++)

, specifically the InvMain.Length part, give a length of 48 (12 x 4). To get the length of the first dimension, it has to be InvMain.GetLength(0):

 for (var x = 0; x < InvMain.GetLength(0); x ++)

This will give you the length of the first dimension (the "rows" of the array). For the second dimension (the "columns"), use InvMain.GetLength(1).

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

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

15 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

Related Questions

Creating an inventory/weapon array and ability to scroll through it 1 Answer

JavaScript Grid Array Size 0 Answers

Is this considered bad coding 1 Answer

Array index is out of range? 1 Answer

Saving Transform Array 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