Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Create inventory from folder

Discussion in 'Editor & General Support' started by afurioso, Jun 30, 2022.

  1. afurioso


    Jan 12, 2022
    Hello everyone,

    I'm new to Unity and I would to know how to build an inventory with the items taken from a prefab folder. And I would give them the same dimension (not scale).

    Thank you in advance!
  2. Kurt-Dekker


    Mar 16, 2013
    Resources.LoadAll<T>() can get you all things in a folder, or you can use Addressables.

    Alternately you can roll your own editor-time mechanism that produces a directory at build time and use that to link stuff.

    Here's my standard inventory notes:

    These things (character customization, inventories, shop systems) are fairly tricky hairy beasts, definitely deep in advanced coding territory. They contain elements of:

    - a database of items that you may possibly possess / equip
    - a database of the items that you actually possess / equip currently
    - perhaps another database of your "storage" area at home base?
    - persistence of this information to storage between game runs
    - presentation of the inventory to the user (may have to scale and grow, overlay parts, clothing, etc)
    - interaction with items in the inventory or on the character or in the home base storage area
    - interaction with the world to get items in and out
    - dependence on asset definition (images, etc.) for presentation

    Just the design choices of an inventory system can have a lot of complicating confounding issues, such as:

    - can you have multiple items? Is there a limit?
    - are those items shown individually or do they stack?
    - are coins / gems stacked but other stuff isn't stacked?
    - do items have detailed data shown (durability, rarity, damage, etc.)?
    - can users combine items to make new items? How? Limits? Results? Messages of success/failure?
    - can users substantially modify items with other things like spells, gems, sockets, etc.?
    - does a worn-out item (shovel) become something else (like a stick) when the item wears out fully?
    - etc.

    Your best bet is probably to write down exactly what you want feature-wise. It may be useful to get very familiar with an existing game so you have an actual example of each feature in action.

    Once you have decided a baseline design, fully work through two or three different inventory tutorials on Youtube, perhaps even for the game example you have chosen above.

    Or... do like I like to do: just jump in and make it up as you go. It is SOFT-ware after all... evolve it as you go! :)

    Breaking down a large problem such as inventory:

    "Combining a bunch of stuff into one line always feels satisfying, but it's always a PITA to debug." - Star Manta on the Unity3D forums
  3. afurioso


    Jan 12, 2022

    Hi Kurt,

    at the moment it is a simple thing. I want to display the objects in a grid layout and I would be able to do that but the real problem is the size. So I thought that an inventory is a good choice because with an UI I can put the icon of the object and the text, but I don't know how to do it.

    Actually, I'm trying with a scriptable object but nothing shows, only the default item UI. Can you help me with the code?

    Thank you!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    5. public abstract class ObjectItem : ScriptableObject
    7. {
    8.         [Header("Company Details")]
    9.         [SerializeField] string owner;
    10.         [SerializeField] int ownerId;
    12.         public Sprite objectImage;
    13.         public GameObject theObject;
    15.         public string objectName;
    16.         [SerializeField] int objectId;
    17.         public ItemType type;
    18.         [SerializeField] string link;
    19.         [TextArea(10,15)]
    20.         [SerializeField] string description;
    22. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    6. public class InventoryManager : MonoBehaviour
    7. {
    8.     public static InventoryManager Instance;
    9.     public List<ObjectItem> Items;
    10.     //public List<GameObject> objectToInstantiate;
    12.     public Transform itemContent;
    13.     public GameObject inventoryItem;
    15.     private void Awake()
    16.     {
    17.         Instance = this;
    19.         //objectToInstantiate = new List<GameObject>(Resources.LoadAll<GameObject>("Target"));
    20.         Items = new List<ObjectItem>(Resources.LoadAll<ObjectItem>("Items"));
    22.         foreach (ObjectItem item in Items)
    23.         {
    24.             GameObject obj = Instantiate(inventoryItem, itemContent);
    26.             var itemName = obj.transform.Find("Object Name").GetComponent<TextMesh>().text;
    27.             var itemIcon = obj.transform.Find("Icon").GetComponent<Image>().sprite;
    29.             itemName = item.objectName;
    30.             itemIcon = item.objectImage;
    33.         }
    34.     }
    35. }
    This is what I see:
  4. Kurt-Dekker


    Mar 16, 2013
    Your best bet for something as hairy as an inventory is to pick a few different inventory tutorials and work through them fully from beginning to end.

    If you prefer to skip that and thunder ahead with the above code, here's how to begin debugging.

    Remember: one thing at a time.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: or this answer for Android:

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    You must find a way to get the information you need in order to reason about what the problem is.