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!
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!
Resources.LoadAll() 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:
Just the design choices of an inventory system can have a lot of complicating confounding issues, such as:
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
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!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public abstract class ObjectItem : ScriptableObject
{
[Header("Company Details")]
[SerializeField] string owner;
[SerializeField] int ownerId;
public Sprite objectImage;
public GameObject theObject;
public string objectName;
[SerializeField] int objectId;
public ItemType type;
[SerializeField] string link;
[TextArea(10,15)]
[SerializeField] string description;
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class InventoryManager : MonoBehaviour
{
public static InventoryManager Instance;
public List<ObjectItem> Items;
//public List<GameObject> objectToInstantiate;
public Transform itemContent;
public GameObject inventoryItem;
private void Awake()
{
Instance = this;
//objectToInstantiate = new List<GameObject>(Resources.LoadAll<GameObject>("Target"));
Items = new List<ObjectItem>(Resources.LoadAll<ObjectItem>("Items"));
foreach (ObjectItem item in Items)
{
GameObject obj = Instantiate(inventoryItem, itemContent);
var itemName = obj.transform.Find("Object Name").GetComponent<TextMesh>().text;
var itemIcon = obj.transform.Find("Icon").GetComponent<Image>().sprite;
itemName = item.objectName;
itemIcon = item.objectImage;
}
}
}
This is what I see: https://prnt.sc/_wJ9cbtY1xR-
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:
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:
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: https://discussions.unity.com/t/700551 or this answer for Android: https://discussions.unity.com/t/699654
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:
https://discussions.unity.com/t/839300/3
You must find a way to get the information you need in order to reason about what the problem is.