Forum begins after the advertisement:


[General] Issues with ItemSlots

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #16809
    Von
    Level 10
    Silver Supporter (Patron)
    Helpful?
    Up
    0
    ::

    A few of the weaponSlots and passiveSlots still showing during gameplay. All the other slots are populated correctly, except for these last 3. Editor

    GamePlay
    #16810
    Von
    Level 10
    Silver Supporter (Patron)
    Helpful?
    Up
    0
    ::

    Here is my UIInventoryDisplay.cs

    using System.Collections.Generic;
    using System;
    using System.Reflection;
    using UnityEngine;
    using UnityEngine.UI;
    using TMPro;
    
    [RequireComponent(typeof(LayoutGroup))]
    public class UIInventoryIconsDisplay : MonoBehaviour
    {
    
        public GameObject slotTemplate;
        public uint maxSlots = 6;
        public bool showLevels = true;
        public PlayerInventory inventory;
    
        public GameObject[] slots;
    
        [Header("Paths")]
        public string iconPath;
        public string levelTextPath;
        [HideInInspector] public string targetedItemList;
    
        void Reset()
        {
            slotTemplate = transform.GetChild(0).gameObject;
            inventory = FindObjectOfType<PlayerInventory>();
        }
    
        void OnEnable()
        {
            Refresh();
        }
    
        // This will read the inventory and see if there are any new updates
        // to the items on the PlayerCharacter.
        public void Refresh()
        {
            if (!inventory) Debug.LogWarning("No inventory attached to the UI icon display.");
    
            // Figure out which inventory I want.
            Type t = typeof(PlayerInventory);
            FieldInfo field = t.GetField(targetedItemList, BindingFlags.Public | BindingFlags.Instance);
    
            // If the given field is not found, then show a warning.
            if (field == null)
            {
                Debug.LogWarning("The list in the inventory is not found.");
                return;
            }
    
            // Get the list of inventory slots.
            List<PlayerInventory.Slot> items = (List<PlayerInventory.Slot>)field.GetValue(inventory);
    
            // Start populating the icons.
            for(int i = 0; i < items.Count; i++)
            {
                // Check if we have enough slots for the item.
                // Otherwise let's print a warning so that our users set this component up properly.
                if(i >= slots.Length)
                {
                    Debug.LogWarning(
                        string.Format(
                            "You have {0} inventory slots, but only {1} slots on the UI.",
                            items.Count, slots.Length
                        )
                    );
                    break;
                }
    
                // Get the item data.
                Item item = items[i].item;
    
                Transform iconObj = slots[i].transform.Find(iconPath);
                if(iconObj)
                {
                    Image icon = iconObj.GetComponentInChildren<Image>();
    
                    // If the item doesn't exist, make the icon transparent.
                    if (!item) icon.color = new Color(1, 1, 1, 0);
                    else
                    {
                        // Otherwise make it visible and update the icon.
                        icon.color = new Color(1, 1, 1, 1);
                        if (icon) icon.sprite = item.data.icon;
                    }
                }
    
                // Set the level as well.
                Transform levelObj = slots[i].transform.Find(levelTextPath);
                if(levelObj)
                {
                    // Find the Text component and put the level inside.
                    TextMeshProUGUI levelTxt = levelObj.GetComponentInChildren<TextMeshProUGUI>();
                    if (levelTxt)
                    {
                        if (!item || !showLevels) levelTxt.text = "";
                        else levelTxt.text = item.currentLevel.ToString();
                    }
                }
            }
        }
    }
    
    #16811
    Alp Apustaja
    Level 1
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    It looks like you have provided the code for your UIInventoryIconsDisplay.cs script. What specific issue are you facing or what help do you need with this code?

    #16812
    Von
    Level 10
    Silver Supporter (Patron)
    Helpful?
    Up
    0
    ::

    Well specifically the portion

    
    // Get the item data.
                Item item = items[i].item;
    
                Transform iconObj = slots[i].transform.Find(iconPath);
                if(iconObj)
                {
                    Image icon = iconObj.GetComponentInChildren<Image>();
    
                    // If the item doesn't exist, make the icon transparent.
                    if (!item) icon.color = new Color(1, 1, 1, 0);
                    else
                    {
                        // Otherwise make it visible and update the icon.
                        icon.color = new Color(1, 1, 1, 1);
                        if (icon) icon.sprite = item.data.icon;
                    }
                }
    

    Where it should be making all of the icons transparent if they dont exist seems to be malfunctioning i think, either that or i am missing something in the editor setup

    #16813
    Alp Apustaja
    Level 1
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    Hello Von (patreon_149496563),

    From the code snippet you provided, it looks like the intention is to make the icon transparent if the item associated with it doesn’t exist. However, I can see a potential issue in the code that might cause unexpected behavior.

    The line if (!item) icon.color = new Color(1, 1, 1, 0); checks if the item is null to determine whether the icon should be transparent. If the item doesn’t exist, the icon’s color is set to transparent. However, it’s important to note that the condition `if (!

    #16814
    Von
    Level 10
    Silver Supporter (Patron)
    Helpful?
    Up
    1
    ::

    I fixed it. smh somehow all my ui elements got depopulated. i had to recreate my prefab and add everything again. working like a charm!

      1 anonymous person
    has upvoted this post.
    #16815
    Alp Apustaja
    Level 1
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    That’s great to hear, Von! I’m glad you were able to fix the issue with your UI elements. Remember, sometimes recreating things from scratch can lead to a cleaner and more efficient solution. If you have any more questions or need further assistance, feel free to ask!

    #16818
    A_DONUT
    Level 6
    Moderator
    Helpful?
    Up
    0
    ::

    Interesting now the bot can do our task interesting

    #16822
    Alp Apustaja
    Level 1
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    Hello, @A_DONUT! How can I assist you today with your programming task?

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

Go to Login Page →


Advertisement below: