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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Troubles with inventory system

Discussion in 'Scripting' started by danilo12, Jul 2, 2017.

  1. danilo12

    danilo12

    Joined:
    Jul 21, 2015
    Posts:
    64
    So I want to make an inventory system, simply because I have never done it before, I thought of all the things I would like it to do, to equip weapons from your inventory and to collect items from dead enemies, also to destroy items..

    First up I made an item class, fairly simple with one stat - damagevalue, it's icon as a sprite and it's ID which I will explain later on why it is useful.
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. [System.Serializable]
    7. public class Item{
    8.  
    9.  
    10.     public int ID;
    11.     public GameObject itemObject;
    12.     public Sprite Icon;
    13.     public float DamageValue;
    14.  
    15. }
    16.  
    Then I made a Slot Script for storing the items within slots in my bag, the UI of the bag consists of a panel on which I have placed on a 4x4 table made up from "Image" UI elements. Each slot has an Image, which is like a reference to the Image appropriate to the specific slot. As well as an item, which is the item that the slot contains;
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6. [System.Serializable]
    7. public class Slot {
    8.     public Item SlotItem;
    9.     public Image SlotImage;
    10. }
    11.  
    Now I have an itemdatabase script, which is actually just a script in which i have a list of all available items, as a List of the "Item" class.

    Last of all, is the script that I am having problems with, the Inventory script itself:

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6.  
    7. public class Inventory : MonoBehaviour {
    8.     public ItemDatabase dbRef; //A reference to the itemdatabase script
    9.     public List<Slot> BagSlots = new List<Slot>(); //List of all the slots in my bag
    10.     bool invUpdated;
    11.     void Start(){
    12.         dbRef = GameObject.FindGameObjectWithTag ("itemDatabase").GetComponent<ItemDatabase> (); //Setting the reference through the itemDB tag
    13.     }
    14.     void Update(){
    15.       UpdateInventory()
    16.  
    17.     }
    18.     void UpdateInventory(){
    19.         //This function is used as an update to get called all the time, it is actually what controls the slots of the specific item
    20.  
    21.         for(int i = 0; i< BagSlots.Count; i++){ //Starting from the first element in BagSlots, till the last
    22.             BagSlots[i].SlotItem = dbRef.itemDatabase[BagSlots[i].SlotItem.ID]; // So I am setting each item to be equal to the Item with the index 'ID'
    23.                                                                                 //The ID of an item is used for me to find the item in the database..
    24.  
    25.             BagSlots [i].SlotImage.sprite = BagSlots [i].SlotItem.Icon; //In this line I am just changing the Sprite, the "icon" in the UI;      
    26.  
    27.         }
    28.     }
    29.      
    30.  
    31. }
    32.  
    33.  
    Now unity throws me an error "ArgumentOutOfRangeException" each time the loop happens and I can't figure out why
     
  2. cstooch

    cstooch

    Joined:
    Apr 16, 2014
    Posts:
    354
    I think you're confusing arrays and lists in how you're adding your items.

    You add to a list with yourListName.Add(whateveryouwanttoadd);

    I'm also not sure what BagSlots.Count is meant to represent in your code here, unless you're setting the size somewhere else, but even then, I think it's best you look at some examples of using lists and storing classes in a list.

    Here's one example: https://stackoverflow.com/questions/8391885/storing-data-into-list-with-class
     
  3. danilo12

    danilo12

    Joined:
    Jul 21, 2015
    Posts:
    64
    I am not trying to add an item in this script, what I'm doing is checking and updating each slot of my inventory bag to change it's icon if the item inside the slot has changed, for example i deleted an item from my inventory so the slot that contained the item should now display a blank image.

    However, I will convert these lists into arrays to see if it works better this way
     
  4. danilo12

    danilo12

    Joined:
    Jul 21, 2015
    Posts:
    64
    And I just converted the lists into arrays, everywhere in which there was "BagSlots.Range" I converted to "BagSlots.Lenght" and it is still giving me the
    "ArgumentOutOfRangeException: Argument is out of range.Parameter name: index" error. What I think would be most crucial in solving this is if someone could tell me what that error actually means ?
     
  5. Afterbrain

    Afterbrain

    Joined:
    Apr 23, 2017
    Posts:
    8
    Hum... How did you initialize the array?
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Well, I would just ditch this entirely and try an Event approach. It'll be much better.
    When you add/delete an item (or move) it in your inventory, it's then that you should update what's there, its icon, etc.. :)

    Even if your code had been working, I would have suggested this. :)