Text value not updating after subtracting from it?

SlimDoughnut

New member
In my Reload function, I'm trying to update my attributeValueText value so it updates to its new value after being called. The problem is the Debug log shows that the value has changed but the UI text never updates and stays the same value no matter how times it's called. I feel like it has something to do with SetValue() but I don't know how I should change it to make it work.

C#:
    public class BulletCounter : ItemViewModule
    {
        [SerializeField] private string attributeName;

        [SerializeField] public Text attributeValueText;

        [SerializeField] protected GameObject disableOnClear;

        public string AttributeName
        {
            get => attributeName;
            set => attributeName = value;
        }

        /// <summary>
        /// Set the value.
        /// </summary>
        /// <param name="info">The item info.</param>
        public override void SetValue(ItemInfo info)
        {

            if (info.Item == null || info.Item.IsInitialized == false)
            {
                Clear();
                return;
            }

            if (!info.Item.TryGetAttributeValue<int>(attributeName, out var value))
            {
                Clear();
                return;
            }

            if (disableOnClear != null)
            {
                disableOnClear.SetActive(true);
            }

            attributeValueText.text = $" {info.Amount * value}";
        }

        public void Reload(ItemInfo info)
        {
            var pistol = info.Inventory.gameObject.GetComponentInChildren<Pistol>();
            var inventoryGrid = InventorySystemManager.GetItemViewSlotContainer<InventoryGrid>(1, "My Inventory Grid Panel");

            int ammo = int.Parse(attributeValueText.text);
           
            attributeValueText.text = $" {ammo - pistol.reloadAmount}";
            inventoryGrid.Draw();


            Debug.Log(pistol.reloadAmount);
            Debug.Log(attributeValueText.text);
        }

        /// <summary>
        /// Clear the value.
        /// </summary>
        public override void Clear()
        {
            if (disableOnClear != null)
            {
                disableOnClear.SetActive(false);
            }
        }
    }
}

Example of value on magazine not changing:
image_2022-05-13_074553231.png
 
I'm not sure I understand.

You are setting the text in two functions "SetValue" and "Reload" with two different forumlas
In "Reload" you are redrawing the Grid which calls "SetValue". So essentially the text you write in Reload is replaced as soon as you draw the grid again.

What you should be doing is everything inside the "SetValue" function. This is called everytime you draw the grid.
 
Top