Inventory problem.

Hey, I am making a platformed game and on the first level there are 6 test tubes that you can pick up, then they will go into the inventory. I made a timer so you can only use something in the inventory along as you are using it at least 1.5 seconds before the last item used. And when you click the item in the inventory it will be removed, what the description says on the item will take effect and it will make a bubble noise also. This all works but sometimes when I click on one of the inventory items it will not make the sound, the description of the item won’t happen for example the potion i clicked slows down time, that won’t happen. And in the console it will say ArgumentOutOfRangeException: Argument is out of range. I don’t know how to fix this any help would be appreciated. Here is my inventory code that is used and an image of the inventory:
[30928-screen+shot+2014-08-14+at+16.09.58.png|30928]

import System.Collections.Generic;

var inventoryBackground : GUISkin;
var inventoryButton : GUISkin;
var itemBackground : GUISkin;

var timer : float;

var bubble : AudioSource;

var items : Item[];

var healthGO : GameObject;

var inventoryOpen : boolean = false;

var mainInventory : List.<Item> = new List.<Item>();

function Update(){
timer += Time.deltaTime;
	if(Input.GetKeyDown(KeyCode.I)){
		inventoryOpen = !inventoryOpen;
	}
}	

function OnGUI(){

//Open and close te inventory
GUI.skin = inventoryButton;
if(GUI.Button(Rect(3,Screen.height-31,100,28),"")){
	inventoryOpen = !inventoryOpen;
}
GUI.skin = null;
//Draw a box around the inventory items
GUI.skin = inventoryBackground;
if(inventoryOpen){
	GUI.Box(Rect(2,Screen.height-389,480,355),"");
}
GUI.skin = null;

	for(var i = 0; i < mainInventory.Count; i++){
	if(inventoryOpen){
	 
/*************** What the buttons do if they are clicked in the inventory ****************/
GUI.skin = itemBackground;
		if(GUI.Button(Rect(13,Screen.height-327+(48*i),40,40),mainInventory*.icon)){*
  •  if(timer > 1.5){*
    
  •  mainInventory.RemoveAt(i);*
    
  •  timer = 0;*
    

_ if(mainInventory*.name == “GreenPotion”){_
_
drankGreenPotion();_
_
}_
_ if(mainInventory.name == “PinkPotion”){
drankPinkPotion();
}
if(mainInventory.name == “OrangePotion”){
drankOrangePotion();
}
if(mainInventory.name == “RedPotion”){
drankRedPotion();
}
if(mainInventory.name == “PurplePotion”){
drankPurplePotion();
}
if(mainInventory.name == “BluePotion”){
drankBluePotion();
}*_

* }*
}
GUI.skin = null;

/************** Show the description, name and rarity of the item in the inventory **************/
* GUI.contentColor = Color.black;*
_ GUI.Label(Rect(55,Screen.height-330+(48i),500,26),"Name : " + mainInventory.name);
GUI.Label(Rect(55,Screen.height-318+(48i),500,26),"Description : " + mainInventory*.description);
GUI.Label(Rect(55,Screen.height-306+(48i),500,26),"Rarity : " + mainInventory*.rarity);
GUI.contentColor = Color.white;*_

* }*
* }*
}

/************ Adding the items to the inventory ************/
function OnTriggerEnter2D(other : Collider2D){
* if(other.tag == “RoundBluePotion”){*
* mainInventory.Add(items[4]);*
* Destroy(other.gameObject);*
* }*
* if(other.tag == “RoundGreenPotion”){*
* mainInventory.Add(items[5]);*
* Destroy(other.gameObject);*
* }*
* if(other.tag == “RoundPinkPotion”){*
* mainInventory.Add(items[3]);*
* Destroy(other.gameObject);*
* }*
* if(other.tag == “RoundRedPotion”){*
* mainInventory.Add(items[1]);*
* Destroy(other.gameObject);*
* }*
* if(other.tag == “RoundPurplePotion”){*
* mainInventory.Add(items[2]);*
* Destroy(other.gameObject);*
* }*
* if(other.tag == “RoundOrangePotion”){*
* mainInventory.Add(items[0]);*
* Destroy(other.gameObject);*
* }*
}

function drankOrangePotion(){
audio.PlayOneShot(bubble.clip);
* var pM = gameObject.GetComponent(PlayerMovement);*
* pM.moveSpeed = 5;*
* yield WaitForSeconds(5);*
* pM.moveSpeed = 2;*
}

function drankBluePotion(){
audio.PlayOneShot(bubble.clip);
* var mP = gameObject.GetComponent(PlayerMovement);*
* mP.jumpspeed = 7.6;*
* yield WaitForSeconds(5);*
* mP.jumpspeed = 3.8;*
}

function drankRedPotion(){
audio.PlayOneShot(bubble.clip);
* var ye = healthGO.gameObject.GetComponent(Health);*
* ye.LIVES += 5;*
}

function drankPurplePotion(){
audio.PlayOneShot(bubble.clip);
* var oy = healthGO.gameObject.GetComponent(Health);*
* oy.LIVES += 10;*
}

function drankPinkPotion(){
audio.PlayOneShot(bubble.clip);
* var alpha = healthGO.gameObject.GetComponent(Health);*
* alpha.LIVES += 1;*
}

function drankGreenPotion(){
audio.PlayOneShot(bubble.clip);
Time.timeScale = 0.5;
yield WaitForSeconds(10);
Time.timeScale = 1;
}

You’re removing items then evaluating the same index position in the list:

 mainInventory.RemoveAt(i);
 timer = 0;

 if(mainInventory*.name == "GreenPotion"){*

drankGreenPotion();
}
Now, the variable i was already set above the the code above in the for loop. If you for instance remove the last item in the list and then it hits your if statements, guess what, the item doesn’t exist, your trying to access the removed item/element in the List.
Example:
If you had 10 items, you go with the last item, the variable i is set to 9, you remove 9, then you try and gain access to a property/field called name… FAIL, your index is out of range for the list.