Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

[FIXED] ArgumentOutOfRangeException: Argument is out of range.

Discussion in 'Scripting' started by 420BlazeIt, Feb 19, 2015.

  1. 420BlazeIt

    420BlazeIt

    Joined:
    Aug 14, 2014
    Posts:
    102
    Here's my script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class Looting : MonoBehaviour {
    6.  
    7.     private int rayLength = 3;
    8.     private bool paused = false;
    9.     public Camera playerCam;
    10.     private GameObject lootedItem;
    11.     public GameObject myM4;
    12.     private DestroyMe dScript;
    13.  
    14.     public bool showInventory;
    15.     public List<Item> inventory = new List<Item>();
    16.     private ItemDatabase database;
    17.  
    18.     private Rect inventoryRect;
    19.     private int inventoryX;
    20.     private int inventoryWidth = 400;
    21.     private int inventoryY;
    22.     private int inventoryHeight = 600;
    23.     private string inventoryTitle = "";
    24.  
    25.     private Rect equippedRect;
    26.     private int equippedX;
    27.     private int equippedWidth = 400;
    28.     private int equippedY;
    29.     private int equippedHeight = 300;
    30.     public List<Item> equippedItem = new List<Item>();
    31.  
    32.     private Rect equippedRect2;
    33.     private int equippedX2;
    34.     private int equippedWidth2 = 400;
    35.     private int equippedY2;
    36.     private int equippedHeight2 = 300;
    37.  
    38.     private Vector2 scrollPosition;
    39.     private int padding = 20;
    40.  
    41.     public GUISkin inventorySkin;
    42.  
    43.     private string equippedItemOne;
    44.     private string equippedItemTwo;
    45.  
    46.     private Item selectedItem;
    47.    
    48.     void Start () {
    49.         if(networkView.isMine) {
    50.             database = GameObject.FindGameObjectWithTag ("ItemDB").GetComponent<ItemDatabase> ();
    51.             showInventory = false;
    52.  
    53.             equippedItemOne = "";
    54.             equippedItemTwo = "";
    55.         } else {
    56.             enabled = false;
    57.         }
    58.     }
    59.  
    60.     void Update () {
    61.         if(Input.GetKeyDown(KeyCode.Escape)) {
    62.             paused = !paused;
    63.         }
    64.  
    65.         if(!paused) {
    66.  
    67.             if(Input.GetKeyDown(KeyCode.Tab)) {
    68.                 showInventory = !showInventory;
    69.             }
    70.        
    71.             RaycastHit hit;
    72.             Ray ray = playerCam.ScreenPointToRay(new Vector3(Screen.width*0.5f, Screen.height*0.5f, 0.0f));
    73.            
    74.             if(Input.GetKeyDown(KeyCode.E)) {
    75.  
    76.                 if(Physics.Raycast(ray, out hit, rayLength)) {
    77.  
    78.                     if(hit.collider.tag == "M4Loot") {
    79.                         lootedItem = hit.transform.gameObject;
    80.  
    81.                         //networkView.RPC("EnableM4", RPCMode.AllBuffered);
    82.  
    83.                         inventory.Add(database.items[0]);
    84.  
    85.                         dScript = lootedItem.GetComponent<DestroyMe>();
    86.                         dScript.destroyMe = true;
    87.                     }
    88.                 }
    89.             }
    90.         }
    91.     }
    92.  
    93.     void InventoryWindow (int windowID) {
    94.         scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUILayout.Width(inventoryWidth - padding), GUILayout.Height(inventoryHeight - padding - 5));
    95.         for(int i = 0; i < inventory.Count; i++) {
    96.             GUILayout.Space(20);
    97.             GUILayout.Label(inventory[i].itemName);
    98.             GUILayout.BeginHorizontal();
    99.             selectedItem = inventory[i];
    100.             if(inventory[i].itemType == Item.ItemType.Equippable) {
    101.                 if(GUILayout.Button("Equip")) {
    102.                     equippedItem.Add(inventory[i]);
    103.  
    104.                     if(equippedItemOne == "") {
    105.                         equippedItemOne = inventory[i].itemName;
    106.                         RemoveItemFrominventory();
    107.                     } else {
    108.                         equippedItemTwo = inventory[i].itemName;
    109.                         RemoveItemFrominventory();
    110.                     }
    111.                 }
    112.                 GUILayout.Space(50);
    113.                 if(GUILayout.Button("Drop")) {
    114.                     RemoveItemFrominventory();
    115.                 }
    116.             }
    117.             if(inventory[i].itemType == Item.ItemType.Consumable) {
    118.                 if(GUILayout.Button("Eat")) {
    119.                     Debug.Log("Eat: " + inventory[i].itemName);
    120.                 }
    121.                 GUILayout.Space(50);
    122.                 GUILayout.Button("Drop");
    123.             }
    124.             if(inventory[i].itemType == Item.ItemType.Clothing) {
    125.                 if(GUILayout.Button("Equip")) {
    126.                     Debug.Log("Equip: " + inventory[i].itemName);
    127.                 }
    128.                 GUILayout.Space(50);
    129.                 GUILayout.Button("Drop");
    130.             }
    131.             GUILayout.EndHorizontal();
    132.         }
    133.         GUILayout.EndScrollView ();
    134.     }
    135.  
    136.     void EquippedWindow (int windowID) {
    137.  
    138.     }
    139.     void EquippedWindow2 (int windowID) {
    140.        
    141.     }
    142.  
    143.     void RemoveItemFrominventory () {
    144.         inventory.Remove (selectedItem);
    145.     }
    146.  
    147.     void OnGUI() {
    148.         if (showInventory) {
    149.             GUI.skin = inventorySkin;
    150.             inventoryX = (Screen.width /2) - (inventoryWidth);
    151.             inventoryY = (Screen.height /2) - (inventoryHeight/2);
    152.             inventoryRect = new Rect (inventoryX, inventoryY, inventoryWidth, inventoryHeight);
    153.             GUI.Window(0, inventoryRect, InventoryWindow, inventoryTitle);
    154.  
    155.             equippedX = (Screen.width /2);
    156.             equippedY = (Screen.height /2) - (equippedHeight);
    157.             equippedRect = new Rect (equippedX, equippedY, equippedWidth, equippedHeight);
    158.             GUI.Window(1, equippedRect, EquippedWindow, equippedItemOne);
    159.  
    160.             equippedX2 = (Screen.width /2);
    161.             equippedY2 = (Screen.height /2);
    162.             equippedRect2 = new Rect (equippedX2, equippedY2, equippedWidth2, equippedHeight2);
    163.             GUI.Window(2, equippedRect2, EquippedWindow2, equippedItemTwo);
    164.             GUI.skin = null;
    165.         }
    166.     }
    167.  
    168.     /*[RPC]
    169.     void EnableM4 () {
    170.         myM4.SetActive(true);
    171.     }*/
    172. }
    I am getting this error:
    ArgumentOutOfRangeException: Argument is out of range.
    Parameter name: index
    System.Collections.Generic.List`1[Item].get_Item (Int32 index) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/List.cs:633)
    Looting.InventoryWindow (Int32 windowID) (at Assets/Scripts/Player/Looting.cs:118)
    UnityEngine.GUI.CallWindowDelegate (UnityEngine.WindowFunction func, Int32 id, UnityEngine.GUISkin _skin, Int32 forceRect, Single width, Single height, UnityEngine.GUIStyle style) (at C:/buildslave/unity/build/artifacts/EditorGenerated/GUI.cs:1443)

    Please help.
     
  2. Defero

    Defero

    Joined:
    Jul 9, 2012
    Posts:
    200
    Maybe this is to blame. You're removing while looping. Try storing items you need to remove, and do it after the for loop that is on line 95.
     
  3. 420BlazeIt

    420BlazeIt

    Joined:
    Aug 14, 2014
    Posts:
    102
    I need you to be more specific on how to actually do that.
     
  4. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    The compiler doesn't like when you remove items from a list while you're looping on this same list.

    It's because you're changing the size of the list. You have two solution:
    - you do as @Defero said, you set a boolean to true if you have to remove something and after the loop, you check if this variable is set to true, if it does, you remove your item. You're not anymore in the loop, so you can change his size

    - if you want to remove several items at the same time is to loop from "list.Count" to 0 (and not what you're doing : 0 to "list.Count"). Because you're decreasing the index, you don't really care about the size of the list.
     
  5. 420BlazeIt

    420BlazeIt

    Joined:
    Aug 14, 2014
    Posts:
    102
    This was the answer I was looking for. Thanks for all the support. :D