Array index is out of range?

I’m writing up a basic inventory script based on an array of GameObjects. This is just coding practise, so I’ll probably just release it when I’m done for others to use. This is my Inventory script, applied to a standard First Person Controller:

using UnityEngine;
using System.Collections;

public class Inventory : MonoBehaviour
{	
	public bool inventoryScreenEnabled = false;
	public bool isHoldingSomething = false;
	public GameObject heldItem = null;
	public int maxSlots = 9;
	public GameObject[] InventoryContents = new GameObject[9];
	
	public Transform holdPos;
	
	void Start()
	{
		Screen.showCursor = false;
		Screen.lockCursor = true;
	}
	
	void Update()
	{
		if (Input.GetKeyDown(KeyCode.M))
		{
			if (inventoryScreenEnabled == false && heldItem == null)
			{
				EnableInventory();
			}
			else if (inventoryScreenEnabled == true)
			{
				DisableInventory();	
			}
		}
		if (Input.GetButtonDown("Fire2") && heldItem != null)
		{
			heldItem.transform.parent = null;
			heldItem.GetComponent<PickUp>().enabled = true;
			heldItem.GetComponent<Rigidbody>().useGravity = true;
			heldItem.GetComponent<Rigidbody>().isKinematic = false;
			heldItem = null;	
		}	
	}
	
	void EnableInventory()
	{
		this.transform.GetComponent<CharacterMotor>().enabled = false;
		this.transform.GetComponent<MouseLook>().enabled = false;
		Camera.mainCamera.GetComponent<MouseLook>().enabled = false;
        inventoryScreenEnabled = true;
        Screen.showCursor = true;
        Screen.lockCursor = false;
	}
	
	void DisableInventory()
	{
		this.transform.GetComponent<CharacterMotor>().enabled = true;
		this.transform.GetComponent<MouseLook>().enabled = true;
		Camera.mainCamera.GetComponent<MouseLook>().enabled = true;
        inventoryScreenEnabled = false;	
		Screen.showCursor = false;
		Screen.lockCursor = true;
	}
	
	void OnGUI()
	{
		if (inventoryScreenEnabled == true)
		{
			GUI.BeginGroup (new Rect (Screen.width / 2 - 150, Screen.height / 2 - 150, 300, 300));
			GUI.Box(new Rect(0,0,300,300), "Inventory");
			for(var i = 0; i < 9; i++)
			{
				if (InventoryContents *!= null)*
  •  		{*
    

_ if (GUI.Button(new Rect(10,30,280,25), InventoryContents*.name))_
_
{_
_
Debug.Log(“Item pressed!”);_
_ heldItem = InventoryContents;
InventoryContents = null;
heldItem.SetActive(true);
heldItem.transform.parent = holdPos;
heldItem.transform.position = holdPos.transform.position;
heldItem.transform.rotation = holdPos.transform.rotation;
heldItem.GetComponent().useGravity = false;
heldItem.GetComponent().isKinematic = true;
DisableInventory();
}
}
}
}
}
}*

And this is the PickUp script, applied to each item that can be added to the inventory:
using UnityEngine;
using System.Collections;_

public class PickUp : MonoBehaviour
{
* public GameObject playerObject;*
* private Inventory inventory;*

* void Start()*
* {*
* inventory = playerObject.GetComponent(); *
* }*

* void OnMouseDown()*
* {*
* if (inventory.inventoryScreenEnabled == false && inventory.heldItem == null)*
* {*
* for(var i = 0; i < inventory.maxSlots; i++)*
* {*
_ if (inventory.InventoryContents == null)
* {
inventory.InventoryContents = this.gameObject;
transform.parent = playerObject.transform;
transform.position = playerObject.transform.position;
enabled = false;
this.gameObject.SetActive(false);
}
}
}
}
}*

It all works fine until I actually pick up an object, at which point I get that error: “Array index is out of range.”
Can anyone tell me what I’m doing wrong?_

Firstly, you’ll want to see what specific line this is happening from (the error report should notify you of which line it came from, as well if you double-click it, it will automatically open that file, scroll to that line, and highlight it for you).

However, an “array index out of bounds” error means that either you’ve tried to access an array member that is less than 0 (which is impossible, as array indices start at 0, and only increase in value from there) or you used a value that is too large.

Given that in the first section of code you used a 9 literal in your for loop (the only place in that section where the array was accessed), the error could no be coming from there.

However, you used the variable “maxSlots” in the second bit of code, which, my best guess, was changed somewhere to be a value that is either negative, or too large for the inventory array.