Forum begins after the advertisement:


[Part 5] Hand slot error

Viewing 15 posts - 1 through 15 (of 20 total)
  • Author
    Posts
  • #11776

    I already did the part 5 of the farming game tutorial as its follow, but when I tried to test the equipped part, I clicked it and the selected tool didn’t showed up in the Hand Slot in Inventory. Please tell me where I wrong?

    Error Hand Slot in Farming Game

    #11805
    Terence
    Keymaster

    Are there any errors shown in the Console?

    #11807

    No, there is no error on the console but the display of the tool when equipped in the Tool Slot of Inventory won’t showed up. But strangely it is showing up in the Status Bar.

    #11819
    Jonathan Teo
    Moderator

    It’s probably related to the functions in UIManager or InventoryManager. What do they look like?

    #11820

    The InventoryManager.cs:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    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(int slotIndex, InventorySlot.InventoryType inventoryType)
        {
            if(inventoryType == InventorySlot.InventoryType.Item)
            {
                //Cache the Inventory slot ItemData from InventoryManager
                ItemData itemToEquip = items[slotIndex];
    
                //Change the Inventory slot to the Hand Slot
                items[slotIndex] = equippedItem;
    
                //Change the Hand Slot to the Inventory Slot
                equippedItem = itemToEquip;
    
            }else
            {
                //Cache the Inventory slot ItemData from InventoryManager
                ItemData toolToEquip = tools[slotIndex];
    
                //Change the Inventory slot to the Hand Slot
                tools[slotIndex] = equippedTool;
    
                //Change the Hand Slot to the Inventory Slot
                equippedTool = toolToEquip;
            }
            //Update the changes to the UI
            UIManager.Instance.RenderInventory();
        }
    
        //Handles movement of item from Hand to Inventory
        public void HandToInventory(InventorySlot.InventoryType inventoryType)
        {
            if(inventoryType == InventorySlot.InventoryType.Item)
            {
                //Iterate through each inventory slot and find an empty slot
                for(int i = 0; i < items.Length; i++)
                {
                    if(items[i] == null)
                    {
                        //Send the equipped item over to its new slot
                        items[i] = equippedItem;
                        //Remove the item from the hand
                        equippedItem = null;
                        break;
                    }
                }
    
            }
            else
            {
                //Iterate through each inventory slot and find an empty slot
                for(int i = 0; i < tools.Length; i++)
                {
                    if(tools[i] == null)
                    {
                        //Send the equipped item over to its new slot
                        tools[i] = equippedTool;
                        //Remove the item from the hand
                        equippedTool = null;
                        break;
                    }
                }
    
            }
            //Update changes in the inventory
            UIManager.Instance.RenderInventory();
        }
    
        // Start is called before the first frame update
        void Start()
        {
    
        }
    
        // Update is called once per frame
        void Update()
        {
    
        }
    }

    The UIManager.cs:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using TMPro;
    
    public class UIManager : MonoBehaviour, ITimeTracker
    {
        public static UIManager Instance { get; private set; }
        [Header("Status Bar")]
        //Tool equip slot on the status bar
        public Image toolEquipSlot;
        //Time UI
        public TextMeshProUGUI timeText;
        public TextMeshProUGUI dateText;
    
    
        [Header("Inventory System")]
        //The inventory panel
        public GameObject inventoryPanel;
    
        //The tool equip slot UI on the Inventory panel
        public HandInventorySlot toolHandSlot;
    
        //The tool slot UIs
        public InventorySlot[] toolSlots;
    
        //The item equip slot UI on the Inventory panel
        public HandInventorySlot itemHandSlot;
    
        //The item slot UIs
        public InventorySlot[] itemSlots;
    
        //Item info box
        public TextMeshProUGUI itemNameText;
        public TextMeshProUGUI 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();
    
            //Add UIManager to the list of objects TimeManager will notify when the time updates
            TimeManager.Instance.RegisterTracker(this);
        }
    
        //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);
    
            //Render the equipped slots
            toolHandSlot.Display(InventoryManager.Instance.equippedTool);
            itemHandSlot.Display(InventoryManager.Instance.equippedItem);
    
            //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;
        }
    
        //Callback to handle the UI for time
        public void ClockUpdate(GameTimestamp timestamp)
        {
            //Handle the time
            //Get the hours and minutes
            int hours = timestamp.hour;
            int minutes = timestamp.minute;
    
            //AM or PM
            string prefix = "AM ";
    
            //Convert hours to 12 hour clock
            if (hours > 12)
            {
                //Time becomes PM 
                prefix = "PM ";
                hours = hours - 12;
                Debug.Log(hours);
            }
    
            //Format it for the time text display
            timeText.text = prefix + hours + ":" + minutes.ToString("00");
    
            //Handle the Date
            int day = timestamp.day;
            string season = timestamp.season.ToString();
            string dayOfTheWeek = timestamp.GetDayOfTheWeek().ToString();
    
            //Format it for the date text display
            dateText.text = season + " " + day + " (" + dayOfTheWeek + ")";
    
        }
    }
    #11825
    Terence
    Keymaster

    Hi Ondra, it doesn’t seem there is anything wrong with your script. You may want to check the Inspector of all relevant GameObjects to see if you forgot to assign anything.

    #11826

    Hello Terence. I did as you asked but I didn’t think I see anything wrong or error. This is the Manager Inspector looks like in mine.

    Manager Inspector

    #11830
    Jonathan Teo
    Moderator

    Hi Ondra,

    Since it looks like the status bar is working and the hand slots seem to be assigned in UIManager, the next step is to check:

    1. toolHandSlot is correctly assigned to the corresponding hand slot (You can click on it in the inspector and the referenced GameObject will be highlighted in the Inspector
    2. The Hand Slot referenced has all its referenced components assigned correctlyHandSlot Reference
    3. Nothing is amiss in the HandInventorySlot and its parent InventorySlot class (especially the Display function)
    #11838

    Hello Jonathan, thank you for your reply.

    1. I’m sorry for this since it seems I kinda forgot but which is the toolHandSlot in the Hierarchy?

    toolHandSlot(?)

    2. Please check if there is anything wrong.

    Hand Slot

    #11839

    Hello Jonathan, thank you for the reply and since it seems I can’t post an image, then I will answer your reply.

    1. It is in the Hierachy of Inventory Panel? If so, which one is it? The Hand Slot or the Inventory Slots?
    2. I did it correctly but missing the Quantity Text.
    3. For my Inventory Slot, it is like this:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.EventSystems;
    
    public class HandInventorySlot : InventorySlot
    {
        public override void OnPointerClick(PointerEventData eventData)
        {
            //Move item from hand to inventory
            InventoryManager.Instance.HandToInventory(inventoryType);
        }
    }
    #11841
    Terence
    Keymaster

    Hi Ondra,

    Somehow your posts keep getting flagged as spam. I apologise for that. I have unflagged your posts with the images, so you should see it above.

    1. Jonathan is referring to the Tool Hand Slot property on your UIManager component. Click on the assigned object in the slot to see what GameObject is assigned to it. You want to make sure that it is the UI GameObject that represents your hand slot.
    2. Do you mean that the Quantity Text property is not revealed? Or that you haven’t assigned a GameObject into Quantity Text?
    3. Looks about right.

    If you continue to run into issues, for a small fee, either me or Jonathan can jump into a video call with you to run through and fix the issues with you.

    #11848

    Hi Terence, thank you for the reply.

    And somehow my reply always failed. I don’t know how that is happens.

    1. It is already correctly assigned which is very weird.
    2. Yeah, I did as the video tutortial did but it wasn’t there or wasn’t exist.

    #11836

    Hi Jonathan, thank you for the reply and answers some of the points.

    1. I’m sorry for it but since english isn’t my native, I didn’t understand it very well by that. Which is the one you referenced?

    My Hierarcy

    2. Please check if anything wrong with it.

    The Hand Slot

    #11837

    Hello Jonathan, thank you for taking your time to reply.

    1. Can you tell me which is it? I kinda forgot.

    toolHandSlot

    2. Please check if there is anything wrong with it.

    Hierarchy

    #11852
    Terence
    Keymaster

    Hi Ondra, we will need to do a video call with you to pinpoint the issue, as it is quite difficult to do it by sharing images.

    If you do decide to do the call, you can contact us at https://terresquall.com/contact

    You can also wait until you have a longer list of questions before you decide to do the call, so you can get your money’s worth!

    Alternatively, do mull over the issue. When I first started learning programming, I would get stuck with a bug for a very long time, but if I kept trying to find a possible fix every day, I would eventually find it. Sometimes it may take a few days or a week, but you will eventually find the fix if you don’t give up!

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

Go to Login Page →


Advertisement below: