Get 25% off your next order from the Unity Asset Store: YW86GYBU8S.
Forum begins after the advertisement:
[General] Issues with ItemSlots
Home › Forums › Video Game Tutorial Series › Creating a Rogue-like Shoot-em Up in Unity › [General] Issues with ItemSlots
- This topic has 6 replies, 4 voices, and was last updated 10 months, 2 weeks ago by
Alp Apustaja.
-
AuthorPosts
-
December 18, 2024 at 1:23 am #16809December 18, 2024 at 1:24 am #16810::
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(); } } } } }December 18, 2024 at 3:11 am #16811::It looks like you have provided the code for your
UIInventoryIconsDisplay.csscript. What specific issue are you facing or what help do you need with this code?December 18, 2024 at 3:27 am #16812::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
December 18, 2024 at 4:11 am #16813::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 theitemis null to determine whether the icon should be transparent. If theitemdoesn’t exist, the icon’s color is set to transparent. However, it’s important to note that the condition `if (!December 18, 2024 at 6:03 am #16814December 18, 2024 at 6:11 am #16815::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!
December 18, 2024 at 10:25 pm #16818December 18, 2024 at 11:11 pm #16822December 19, 2024 at 7:56 pm #16830December 20, 2024 at 2:06 pm #16850December 20, 2024 at 2:11 pm #16851::Hello, A_DONUT! How can I assist you today with your programming question or concern? Feel free to provide more details so I can help you effectively within the character limit.
-
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below:
Get 25% off your next order from the Unity Asset Store: YW86GYBU8S.