Changing UI Sprite Prevents Me From Changing The Color?

I have been working on an issue for the past 3 days that I have not been able to get past. I have a couple of scripts implemented using IDragHandler and OnTriggerEnter2D that turn items in the inventory red when they are dragged overtop of a pre-occupied space in the inventory. This works great for my potion pickup test item, but it does not work for my coin pickup. Through playtesting I have identified that it does not work on my coin item because the sprite is changed as more coins are added, and for whatever reason changing the sprite seems to disable any way to change the color while playing live. I haven’t been able to identify a way to get around this. Below I will post all the relevant script snippets and a picture that shows that the inspector is reading the color change but it is not showing up in-game.

Any help is greatly appreciated thanks!

Coin Sprite Changer:

    public void UpdateCoinCount()
    {
        _coinText.text = "" + curValue;

        if (curValue == 1)
        {
            _image.sprite = _coinSprites[0];

        }
        else if (curValue > 1 && curValue < 30)
        {
            _image.sprite = _coinSprites[1];
        }
        else if (curValue >= 30 && curValue < 75)
        {
            _image.sprite = _coinSprites[2];
        }
        else
        {
            _image.sprite = _coinSprites[3];
        }
    }

Item Drag Handler Contact:

    //CONTACT METHODS
    public void ItemContactArt()
    {
        _itemImage.color = _contactColor;
    }
    public void ItemDefaultArt()
    {
        _itemImage.color = Color.white;
    }

Item Slot (Used for OnTriggerEnter2D):

            if (other.gameObject.CompareTag("InventorySlot"))
            {
                InventorySlot targetInvSlot = other.gameObject.GetComponent<InventorySlot>();

                if (targetInvSlot.isOccupied)
                {
                    _parentItem.ItemContactArt();
                }
                else if (!targetInvSlot.isOccupied) //If the inventory slot is empty
                {
                    //TODO: Add visual effect if blocked
                    _parentItem.inventorySlotsToBeFilled.Add(targetInvSlot);
                    _parentItem.targetInventory = targetInvSlot.parentInventory; //Sets the target InvClass
                    slotCanBePlaced = true;

                    if (_isTopSlot) //If it is the top slot set it as where to place the item
                    {
                        _parentItem.targetSlot = targetInvSlot;
                    }
                }

SOLVED: I was able to solve this issue by using Image.spriteOverride when originally assigning the new coin sprites instead of just Image.sprite. Not exactly sure why this solved the issue, but if anyone else runs into this hope this helps!

Known issue: Unity Issue Tracker - Image Color cannot be changed via script when Image type is set to Simple

1 Like