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 DoctorSauce · Jul 25, 2014 at 09:29 PM · c#nullreferenceexceptioninventory

NRE when actually checking if the object is null?

Okay, NRE's are my weakspot, so this question will probably sound ridiculous to some people. I have this little part of my script, which is supposed to move all the Transforms in a Transform array (Contents) up the array after one of the Transforms has been removed. Hopefully that made sense. Here is that part of the script:

 int ind = 0;
 foreach (Transform trans in Contents){
     if (Contents[ind] == null){
          Debug.Log ("This thing is null");
          if (Contents[ind + 1] != null){
 
         Contents[ind] = Contents[ind + 1];
         Contents[ind + 1] = null;
 
          }
     }
     ind++;
 }


So I run the game, get to the part where this function is called, and it gives me this NRE:

IndexOutOfRangeException: Array index is out of range. Inventory.RemoveItem (UnityEngine.Transform Item) (at Assets/Inventory/InvC#/Inventory.cs:97) Character.EquipItem (.Item i, Int32 slot) (at Assets/Inventory/InvC#/Character.cs:144) Character.DisplayCSheetWindow (Int32 windowID) (at Assets/Inventory/InvC#/Character.cs:258) UnityEngine.GUI.CallWindowDelegate (UnityEngine.WindowFunction func, Int32 id, UnityEngine.GUISkin _skin, Int32 forceRect, Single width, Single height, UnityEngine.GUIStyle style) (at C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/GUI.cs:1402)

I know what this NRE means, that's not the hard part. The part I don't understand is why it's coming up! This is the line it points to:

 if (Contents[ind + 1] != null){

Why is it telling me that the array index is out of range, while I'm checking to see if it's out of range?

This, needless to say, kind of screws up my whole script. And I can't figure out how to fix it! This NRE looks like it just shouldn't be there. Does anybody have any ideas how I can fix this? Thank you in advance for any help offered!

P.S. I am using the C# release of Brackey's inventory system, which since it is unofficial, comes with a lot of errors that I've had to fix. Just in case you're wondering or need more background. I have added this part because the other method of removing items from the inventory just, frankly, didn't work.

Edit: Well, after posting my question, it took me about two seconds to realize part of what the problem is. Rather than checking if Contents[ind + 1] is null, I should be checking if Contents[ind + 1] even exists/is it out of range. So.... my new question is basically, how do I check if an array index is out of range in scripting? Like is there a bool variable stating whether or not an array index is out of range that I can check?

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
Best Answer

Answer by DoctorSauce · Jul 25, 2014 at 09:40 PM

Hmm... I really should have taken longer before posting this question, because I just figured it out. Changed the script to this:

 int ind = 0;
 foreach (Transform trans in Contents){
     if (Contents[ind] == null){
          Debug.Log ("This thing is null");
          if ((ind + 1) < Contents.Length){
  
         Contents[ind] = Contents[ind + 1];
         Contents[ind + 1] = null;
  
          }
     }
     ind++;
 }

And that took care of it! So yeah.... there ya go, anybody else who needs a solution like this! :)

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 gjf · Jul 26, 2014 at 11:03 AM 0
Share

why not store your items in a List ins$$anonymous$$d of an array.

 private List<Transform> _contents;

in Awake()/Start()

 _contents = new List<Transform>();

then to add items:

 _contents.Add(transformOfItem);

and to remove:

 _contents.Remove(indexOfItem);

and not need to shuffle items when you find a null.

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

23 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

Related Questions

Array items null but appear normal in inspector 2 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

C# syntax seems fine, why wont yeild work? (Unsolved) 1 Answer

unity3d simple inventory 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