Forum begins after the advertisement:

 


[Part 5] Slot inventory

Viewing 15 posts - 1 through 15 (of 28 total)
  • Author
    Posts
  • #18505
    Princess Pixie
    Level 3
    Participant
    Helpful?
    Up
    0
    ::

    Hi, I am on part 5: Equpping Items. I am stuck on the part where it assgins the tool or item. In the debug menu it says that all of my slots for tool and items are all 0. I dont know what do to at this point and I can’t go any further without this working. I have the all assign to the right tool slot but its not coming up in the debug menu just as 0 for both item and tools.

    #18506
    Tarha Apustaja
    Level 5
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    Based on the reference information, specifically the comment from Kraege, I recommend:

    Check how the slots are assigned to UIManager in the Inspector. Verify the toolSlots and itemSlots values correspond to the right slots. Enable ‘Debug mode’ in the inspector and check the slotIndex value of each InventorySlot. Only the first slots should have a value of 0. If not, the problem might be in the AssignSlotIndexes() function in UIManager.

    Can you confirm you’ve checked these specific points in your Unity Inspector?

    #18508
    Terence
    Level 31
    Keymaster
    Helpful?
    Up
    0
    ::

    Hi Princess, if you still have problems after following the suggestions from Tarha, do post your code here so that we can take a look and help you comb it for any issues.

    #18518
    Princess Pixie
    Level 3
    Participant
    Helpful?
    Up
    0
    ::

    Thank you for replying. Sorry it took so long to reply. Here is the link to my picture of the slots and debug. https://imgur.com/a/ZdhHao2

    This is the code I have for the UIManager script.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI; 
    
    public class UIManager : MonoBehaviour
    {
        public static UIManager Instance { get; private set; }
        [Header("Status Bar")]
        //Tool equip slot on the status bar
        public Image toolEquipSlot; 
    
    
    
        [Header("Inventory System")]
        //The inventory panel
        public GameObject inventoryPanel; 
    
        //The tool slot UIs
        public InventorySlot[] toolSlots;
    
        //The item slot UIs
        public InventorySlot[] itemSlots;
    
        //Item info box
        public Text itemNameText;
        public Text itemDescriptionText; 
    
        private void Awake()
        {
            //If there is more than one instance, destroy the extra
            if (Instance != null && Instance != this)
            {
                Destroy(this);
            }
            else
            {
                //Set the static instance to this instance
                Instance = this;
            }
        }
    
        private void Start()
        {
            RenderInventory();
            AssignSlotIndexes();
        }
    
    
        //Iterate through the slot UI elements and assign it its reference slot index
        public void AssignSlotIndexes()
        {
            for (int i =0; i<toolSlots.Length; i++)
            {
                toolSlots[i].AssignIndex(i);
                itemSlots[i].AssignIndex(i);
            }
        }
    
    
        //Render the inventory screen to reflect the Player's Inventory. 
        public void RenderInventory()
        {
            //Get the inventory tool slots from Inventory Manager
            ItemData[] inventoryToolSlots = InventoryManager.Instance.tools;
    
            //Get the inventory item slots from Inventory Manager
            ItemData[] inventoryItemSlots = InventoryManager.Instance.items;
    
            //Render the Tool section
            RenderInventoryPanel(inventoryToolSlots, toolSlots);
    
            //Render the Item section
            RenderInventoryPanel(inventoryItemSlots, itemSlots);
    
            //Get Tool Equip from InventoryManager
            ItemData equippedTool = InventoryManager.Instance.equippedTool;
    
            //Check if there is an item to display
            if (equippedTool != null)
            {
                //Switch the thumbnail over
                toolEquipSlot.sprite = equippedTool.thumbnail;
    
                toolEquipSlot.gameObject.SetActive(true);
    
                return;
            }
    
            toolEquipSlot.gameObject.SetActive(false);
    
        }
    
        //Iterate through a slot in a section and display them in the UI
        void RenderInventoryPanel(ItemData[] slots, InventorySlot[] uiSlots)
        {
            for (int i = 0; i < uiSlots.Length; i++)
            {
                //Display them accordingly
                uiSlots[i].Display(slots[i]);
            }
        }
    
        public void ToggleInventoryPanel()
        {
            //If the panel is hidden, show it and vice versa
            inventoryPanel.SetActive(!inventoryPanel.activeSelf);
    
            RenderInventory();
        }
    
        //Display Item info on the Item infobox
        public void DisplayItemInfo(ItemData data)
        {
            //If data is null, reset
            if(data == null)
            {
                itemNameText.text = "";
                itemDescriptionText.text = "";
    
                return;
            }
    
            itemNameText.text = data.name;
            itemDescriptionText.text = data.description; 
        }
    
    }
    #18519
    Tarha Apustaja
    Level 5
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    Based on the reference information and the provided code, I notice a potential issue:

    The code looks correct, but the problem might be in how InventoryManager is populating its tools and items arrays. Can you confirm that:

    1. InventoryManager’s tools and items arrays are being properly initialized
    2. You have actually added items to these arrays before RenderInventory() is called

    Could you show me the InventoryManager script, specifically the part where tools and items are populated?

    #18520
    Princess Pixie
    Level 3
    Participant
    Helpful?
    Up
    0
    ::
    public class InventoryManager : MonoBehaviour
    {
        public static InventoryManager Instance { get; private set; }
    
        private void Awake()
        {
            //If there is more than one instance, destroy the extra
            if(Instance != null && Instance != this)
            {
                Destroy(this);
            }
            else
            {
                //Set the static instance to this instance
                Instance = this; 
            }
        }
    
        [Header("Tools")]
        //Tool Slots
        public ItemData[] tools = new ItemData[8];
        //Tool in the player's hand
        public ItemData equippedTool = null; 
    
        [Header("Items")]
        //Item Slots
        public ItemData[] items = new ItemData[8];
        //Item in the player's hand
        public ItemData equippedItem = null;
    
        //Equipping
    
        //Handles movement of item from Inventory to Hand
        public void InventoryToHand()
        {
    
        }
    
        //Handles movement of item from Hand to Inventory
        public void HandToInventory()
        {
    
        }
    
    
        // Start is called before the first frame update
        void Start()
        {
    
        }
    
        // Update is called once per frame
        void Update()
        {
    
        }
    }
    #18521
    Tarha Apustaja
    Level 5
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    Based on the reference information and the provided script, I notice that the tools and items arrays are initialized but not populated with any ItemData.

    The issue is likely that no ItemData has been assigned to the tools or items arrays. You’ll need to manually assign ItemData to these arrays in the Unity Inspector or through a script.

    To resolve this:

    1. Drag and drop ItemData scriptable objects into the tools and items arrays in the Inspector
    2. Or create a method in Start() to programmatically populate these arrays with ItemData

    Would you like me to show you an example of how to populate these arrays?

    #18522
    Princess Pixie
    Level 3
    Participant
    Helpful?
    Up
    0
    ::

    So with #1 Drag and drop ItemData scriptable objects into the tools and items arrays in the Inspector. So I would drop the ItemData script into the tools and items in the assets data folder.

    Is that correct?

    Sorry I just want to make sure I am understanding it before doing it.

    #18523
    Tarha Apustaja
    Level 5
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    Not quite. You need to drag ScriptableObject instances (like the ‘Cabbage’ item mentioned in the tutorial) from the Assets/Data/Items or Assets/Data/Tools folder into the tools and items arrays in the InventoryManager component in the Inspector.

    Do you understand the difference between a script and a ScriptableObject instance?

    #18524
    Princess Pixie
    Level 3
    Participant
    Helpful?
    Up
    0
    ::

    Can you explain them to me please. Sorry and thank you.

    #18525
    Tarha Apustaja
    Level 5
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    Here’s a clear explanation:

    • Script: A code file (.cs) that defines behavior and properties
    • ScriptableObject: A data container created from a script, which can be saved as an asset in your project

    For ItemData:

    1. ItemData is a script that defines the structure of an item
    2. A ScriptableObject instance of ItemData would be an actual item like “Hoe”, “Cabbage”, etc.

    To create a ScriptableObject:

    1. Right-click in Project window
    2. Choose Create > ItemData
    3. Fill in item details
    4. Drag this new item into InventoryManager’s tools/items arrays

    Would you like me to walk you through creating a ScriptableObject instance step-by-step?

    #18529
    Terence
    Level 31
    Keymaster
    Helpful?
    Up
    0
    ::

    @princesspixie did you manage to solve the issue? Tarha is saying you need to assign the scriptable objects into your InventoryManager component on your Scene (via the Inspector).

    Let me know if this clarifies things.

    #18530
    Jonathan Teo
    Level 18
    Moderator
    Helpful?
    Up
    0
    ::

    If it still doesn’t work, do take a screenshot of what your InventoryManager component looks like

    #18555
    Princess Pixie
    Level 3
    Participant
    Helpful?
    Up
    0
    ::

    Sorry my account locked out for a couple of days. I think I did what you said but here is a screenshot of what I am.

    View post on imgur.com
    #18556
    Princess Pixie
    Level 3
    Participant
    Helpful?
    Up
    0
    ::

    Is there a part of the video where I can go back and watch what he did to understand what I need to do? Thank you.

Viewing 15 posts - 1 through 15 (of 28 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: