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?

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! :slight_smile: