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

Inventory, List = List? please help

Discussion in 'Scripting' started by Ncon123, Sep 8, 2015.

  1. Ncon123

    Ncon123

    Joined:
    May 6, 2015
    Posts:
    25
    I'm following a video series on how to make an inventory.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class Inventory : MonoBehaviour
    6. {
    7.     public List<Item> inventory = new List<Item>();
    8.     public List<Item> slots = new List<Item>();
    9.  
    10.     private ItemDatabase database;
    11.     public GUISkin GUIskin;
    12.     public int slotsX, slotsY;
    13.  
    14.     private bool showInventory;
    15.  
    16.     void Start ()
    17.     {
    18.         for (int i = 0; i > (slotsX * slotsY); i++)
    19.         {
    20.             slots.Add(new Item());
    21.             inventory.Add (new Item());
    22.         }  
    23.         database = GameObject.FindGameObjectWithTag ("Item Database").GetComponent<ItemDatabase>();
    24.  
    25.         inventory.Add(database.items[0]);
    26.     }
    27.  
    28.     void Update ()
    29.     {
    30.         if (Input.GetButtonDown ("Inventory"))
    31.         {
    32.             showInventory = !showInventory;
    33.         }
    34.     }
    35.  
    36.     void OnGUI()
    37.     {
    38.         GUI.skin = GUIskin;
    39.  
    40.         if (showInventory)
    41.         {
    42.             DrawInventory();
    43.         }
    44.     }
    45.  
    46.     void DrawInventory() // MAKING GUI FOR SLOTS
    47.     {
    48.         int i = 0;
    49.  
    50.         for(int x = 0; x < slotsX; x++)
    51.         {
    52.             for(int y = 0; y < slotsY; y++)
    53.             {
    54.                 Rect slotRect = new Rect(x * 100, y * 100, 100,100);
    55.                 GUI.Box(slotRect, "", GUI.skin.GetStyle("Slot"));
    56.  
    57.                     slots[i] = inventory[i];
    58.  
    59.             if(slots[i].itemName != null)
    60.                 {
    61.                     print("creating slot WITH image");
    62.                     GUI.DrawTexture(slotRect,slots[i].itemIcon);
    63.                 }
    64.  
    65.                 i++;  
    66.             }
    67.         }
    68.     }
    69. }
    70.  
    BASICLY when I click "i" It only creates 1 gui box and I get 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)
    Inventory.DrawInventory () (at Assets/Scripts/Inventory.cs:59)
    Inventory.OnGUI () (at Assets/Scripts/Inventory.cs:42)

    and

    NullReferenceException: Object reference not set to an instance of an object
    Inventory.Start () (at Assets/Scripts/Inventory.cs:25)
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    The for loop in the start method has a wrong condition. It should be 'i < ... ' and not 'i > ...' in your case.

    Also, don't rely on other values than the list's or array's length when iterating over it. This will avoid a lot of annoying errors. (I'm referring to the loops in the DrawInventory method).
     
  3. Ncon123

    Ncon123

    Joined:
    May 6, 2015
    Posts:
    25
    Thanks