Inventory (592655)

Hello!

I found a inventory script on Youtube (link).
I added some functions and fixed some problems,
but there is one problem left.
I searched for the “error” for days. Until now i can only
delimit the problem. I think i need some help.

Mainly this script got two for - loops within one if statement.
I have remove the if from both for loops and change it also
into a for loop and insert it after both for loops.

I think i didn’t change the script noticeable but now
I can’t create the inventory two times.
(there is a button and a start void which start the CreateInventory void)

Original (the item added to the prefab while both for loops (for loops create all slots))
Original Scriptpart

    public void CreateInventory()
    {
        foreach(Transform t in this.transform){
            Destroy(t.gameObject);
        }
        for (int x = 1; x <= invetorySize.x; x++) {
            for(int y = 1; y <= invetorySize.y; y ++){
                GameObject slot = Instantiate(slotPrefab) as GameObject;
                slot.transform.SetParent(this.transform);
                slot.name = "slot_"+x+"_"+y;
                slot.GetComponent<RectTransform>().anchoredPosition = new Vector3(windowSize.x/(invetorySize.x+1)*x, windowSize.y / (invetorySize.y+1) * -y);
                //print ("hello "+GameDB.sortedItems.Count);
                if((x + (y - 1)*4) <= GameDB.sortedItems.Count){
                   
                    GameObject item = Instantiate(itemPrefab) as GameObject;
                    item.transform.SetParent(slot.transform);
                    item.GetComponent<RectTransform>().anchoredPosition = Vector3.zero;
                    Item i = item.GetComponent<Item>();
                   
                    //ITEM COMPONENT VARIABLES
                    i.name = GameDB.sortedItems[(x + (y - 1)*4) - 1].name;
                    i.type = GameDB.sortedItems[(x + (y - 1)*4) - 1].type;
                    i.sprite = GameDB.sortedItems[(x + (y - 1)*4) - 1].sprite;
                   
                    item.name = i.name;
                    item.GetComponent<Image>().sprite = i.sprite;
                   
                   
                }
               
            }
        }
       
    }
    public void CreateInventory()
    {
        foreach(Transform t in this.transform){
            Destroy(t.gameObject);
        }
        for (int x = 1; x <= invetorySize.x; x++) {
            for(int y = 1; y <= invetorySize.y; y ++){
                GameObject slot = Instantiate(slotPrefab) as GameObject;
                slot.transform.SetParent(this.transform);
                slot.name = "slot_"+(x + (y -1) * 4); //rename
                slot.GetComponent<RectTransform>().anchoredPosition = new Vector3(windowSize.x/(invetorySize.x+1)*x, windowSize.y / (invetorySize.y+1) * -y);

                // remove if
            }
        }

        // if into for
        for (int f = 1; f <= GameDB.sortedItems.Count; f++) {
            GameObject item = Instantiate(itemPrefab) as GameObject;
            item.transform.SetParent(GameObject.Find("slot_"+f).transform); //changing path
            item.GetComponent<RectTransform>().anchoredPosition = Vector3.zero;
            Item i = item.GetComponent<Item>();

            i.name = GameDB.sortedItems[(f - 1)].name;
            i.type = GameDB.sortedItems[(f - 1)].type;
            i.sprite = GameDB.sortedItems[(f - 1)].sprite;
           
            item.name = i.name;
            item.GetComponent<Image>().sprite = i.sprite;
        }

    }

So why the whole functionality die after my “if into for” - transformation?
I hope you get enough infromation the help me.
(alternative i search for good inventory scripts for free)

have a good day!

You need to switch all of your loops over to starting from zero and say “< maximum” instead of “<= maximum”. Array indexes start from zero, and when you mess with the index counting in this manner, you’re just asking for a mistake somewhere. As it is, it’s too hard to follow the logic for me to be able to tell where a problem might be occurring. Switch all loops to start from zero and say “name = index + 1” when it’s necessary.

Edit: In this:

slot.name="slot_"+(x +(y -1)*4);

X and Y are backwards. After you make the starting loop counters 0, this should be:

slot.name = "slot_" + (1 + (y + x * invetorySize.y));

can you explain how it “dies”… errors? does something it isn’t supposed to? does nothing? etc.

and what is GameDB?

(can’t view the youtube vid :stuck_out_tongue: )

I’m guessing that due to the names being screwed up from having X and Y backwards (and getting results like iteration 2 being marked as “slot_5” as a result), the “Find” function in the second half is not finding anything and causing an error there when he tries to access its transform.

Not that this is a good way of accessing these objects to begin with…

@DonLoquacious i didnt understand why i should start the for loops at zero. my first slot should called “slot_1” and the GameDB.sortedItems could be + 1 as well, or? unitl now i haven’t any other array in this script. I also thought about that the find-methode is to slow for this query. do you got any alternative? i tought about an array which save the current transform (this.transform)

@LeftyRighty He “die’s” means the void CreateInventory() delete the current inventory with item prefabs, set slots, set new item prefabs with correct content but didn’t set the parent to a slot. so everthing is correct and loaded but didn’t displayed. (i will upload my project. maybe someone can take a look to get a better overview)

GameDB.cs

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class GameDB : MonoBehaviour {

    public Sprite[] sprites;

    public static List<Item> allItems = new List<Item>();
    public static List<Item> sortedItems = new List<Item>();

    // Use this for initialization
    void Awake () {

        //ITEM CREATION
        Item i0 = gameObject.AddComponent<Item>();
        i0.name = "Golden Sword";
        i0.type = Item.Type.equip;
        i0.sprite = sprites[0];
        allItems.Add(i0);

        //ITEM CREATION
        Item i1 = gameObject.AddComponent<Item>();
        i1.name = "Health Potion";
        i1.type = Item.Type.consumable;
        i1.sprite = sprites[1];
        allItems.Add(i1);

        //ITEM CREATION
        Item i2 = gameObject.AddComponent<Item>();
        i2.name = "Rejuvenation Potion";
        i2.type = Item.Type.consumable;
        i2.sprite = sprites[2];
        allItems.Add(i2);

        //ITEM CREATION
        Item i3 = gameObject.AddComponent<Item>();
        i3.name = "Bow";
        i3.type = Item.Type.equip;
        i3.sprite = sprites[3];
        allItems.Add(i3);

        //ITEM CREATION
        Item i4 = gameObject.AddComponent<Item>();
        i4.name = "Iron Sword";
        i4.type = Item.Type.equip;
        i4.sprite = sprites[4];
        allItems.Add(i4);

        SortAllItems();
    }
  
    // Update is called once per frame
    void Update () {
  
    }
    public void SortAllItems(){
        sortedItems.Clear();
        foreach(Item i in allItems){
                sortedItems.Add(i);
        }
    }

    public void SortItemsByType(string type){
        sortedItems.Clear();
        foreach(Item i in allItems){
            if(i.type.ToString() == type)
                sortedItems.Add(i);
        }
    }
}

Download my actual project: (press on play and afterwards on “All”)

2240341–149439–Inventory Tutorial.zip (555 KB)

I’m downloading now, so it does not display for you?? That is the only problem???

Edit:
I looked at it, I see the display which is fine, but soon as I click equipment they all disappear… I need more info, because I want to be sure I know what your looking to fix. Now any inventory to work correctly can mean alot more than this… I only see slots, with some items in them, soon as I click equipment, it disappears, no idea what your trying to do…

But… I can tell you that even if this is a basic inventory system this is wrong… So please explain to me,what your trying to fix and do…