- Home /
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:
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[i].icon)){
if(timer > 1.5){
mainInventory.RemoveAt(i);
timer = 0;
if(mainInventory[i].name == "GreenPotion"){
drankGreenPotion();
}
if(mainInventory[i].name == "PinkPotion"){
drankPinkPotion();
}
if(mainInventory[i].name == "OrangePotion"){
drankOrangePotion();
}
if(mainInventory[i].name == "RedPotion"){
drankRedPotion();
}
if(mainInventory[i].name == "PurplePotion"){
drankPurplePotion();
}
if(mainInventory[i].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+(48*i),500,26),"Name : " + mainInventory[i].name);
GUI.Label(Rect(55,Screen.height-318+(48*i),500,26),"Description : " + mainInventory[i].description);
GUI.Label(Rect(55,Screen.height-306+(48*i),500,26),"Rarity : " + mainInventory[i].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;
}
I'm not really sure on javascript, but anyway, does the console tell which line does the error comes from? It'll be much easier to check.
There must be another script where is Item's definition and where are "name" properties added to items array. Show it.(sorry for my English)
Here is my other script for the item's definition here:
class Item {
var name : String;
var icon : Texture2D;
var description : String;
var rarity : Rarity;
var itemOfClothing : ItemOfClothing;
}
enum Rarity {
common,
rare,
awesome
}
enum ItemOfClothing {
shoes,
trousers,
shirt,
hat,
notClothingItem
}
And the error in the console says this:
ArgumentOutOfRangeException: Argument is out of range. Parameter name: index System.Collections.Generic.List`1[Item].get_Item (Int32 index) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.Collections.Generic/List.cs:633) Inventory.OnGUI () (at Assets/Inventory/Inventory.js:51)
I'm guessing that's line 51 but I can't find anything wrong with it...
Answer by Landern · Aug 15, 2014 at 12:15 PM
You're removing items then evaluating the same index position in the list:
mainInventory.RemoveAt(i);
timer = 0;
if(mainInventory[i].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.
So would I need to not use the if statement which checks the name then?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
OnGUI mystically stopped rendering buttons and text. 1 Answer
Inup.GetButtonDown not working? 1 Answer