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

Arugument out of range for Inventory system (JavaScript)

Discussion in 'Scripting' started by seas726, Aug 22, 2016.

  1. seas726

    seas726

    Joined:
    Aug 20, 2016
    Posts:
    2
    I have an inventory system with a list of items that I can edit in script or in the editor. These items have an int variable itemId. In-game, I have it set to transfer an item from my items list to a mainInventory list of type item. When in close enough proximity to the item and if pressing "f" the item should appear in my inventory UI. The problem I have is reading the Id of the in-game item. I attached a script to the itmes I want to pick up, containing a single int variable (also itemId) hoping to be able to read that in relation to the variable on my items list and find a matching Id. It is probably a very simple mistake, but I am truly stumped, and any help would be greatly appreciated. Thanks! (Check line 20 for the error line)
    Code (JavaScript):
    1. var manager : GameObject;
    2. var hand : GameObject;
    3. var oldWeapon : GameObject;
    4. var itemToPickUp : GameObject;
    5. var originalScale : Vector3;
    6. var canPickUp : boolean;
    7. public var isEquipped : boolean;
    8. var isWorking : boolean;
    9. var itemID : int;
    10. var items : Item[];
    11.  
    12. function Update () {
    13. ////////////////////////////////////////////////////////////////
    14. var mainInventory = manager.GetComponent(inventory).mainInventory;
    15. var items = manager.GetComponent(inventory).items;
    16. /////////////////////////////////////////////////////////////
    17. originalScale = transform.localScale;
    18.  
    19. transform.localScale = originalScale;
    20. ////////////////////////////////////////////////////////////
    21. for(var a = 0; a < 2; a++){
    22.     if(itemToPickUp.GetComponent(itemId).itemId == items[a].itemId){
    23.  
    24.         isWorking = true;
    25.         itemID = items[a].itemId;
    26.         }
    27. }
    28. /////////////////////////////////////////////////////////////
    29. if(canPickUp && Input.GetKeyDown("f")){
    30.  
    31.     mainInventory.Add(items[itemID]);
    32.  
    33.     //Destroy (itemToPickUp);
    34.  
    35. }
    36. ////////////////////////////////////////////////////////////////////
    37. if(hand.transform.childCount == 1){
    38.  
    39.     isEquipped = true;
    40.  
    41.     }
    42. }
    43. //////////////////////////////////////////////////////////////////////
    44. function OnTriggerEnter (other : Collider){
    45.  
    46.     if(other.gameObject.tag == "Weapon"){
    47.  
    48.         canPickUp = true;
    49.  
    50.         itemToPickUp = other.gameObject;
    51.         }
    52.     }
    53. //////////////////////////////////////////////////////////////////////
    54. function OnTriggerExit (other : Collider){
    55.  
    56.     if(other.gameObject.tag == "Weapon"){
    57.  
    58.         canPickUp = false;
    59.  
    60.         //itemToPickUp = other.gameObject;
    61.  
    62.  
    63.         }
    64.     }
    65. //////////////////////////////////////////////////////////////////    /////////////////////////////
    66. function OnGUI (){
    67.  
    68. if(canPickUp){
    69.     GUI.Button(Rect(Screen.width/2 - 50,Screen.height/2 + 10 ,125,25), itemToPickUp.itemId);
    70.     }
    71.  
    72. }
    73.  
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,139
    GetComponent(itemId) should be GetComponent<itemId>()

    Assuming your script name is itemId. Otherwise, you need itemid to be replaced with the script name
     
    seas726 likes this.
  3. seas726

    seas726

    Joined:
    Aug 20, 2016
    Posts:
    2
    Thanks, works perfect now!