Search Unity

Changing UI Sprite Prevents Me From Changing The Color?

Discussion in 'UGUI & TextMesh Pro' started by JamieRoss95, Apr 23, 2019.

  1. JamieRoss95

    JamieRoss95

    Joined:
    Apr 5, 2016
    Posts:
    28
    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:

    Code (CSharp):
    1.     public void UpdateCoinCount()
    2.     {
    3.         _coinText.text = "" + curValue;
    4.  
    5.         if (curValue == 1)
    6.         {
    7.             _image.sprite = _coinSprites[0];
    8.  
    9.         }
    10.         else if (curValue > 1 && curValue < 30)
    11.         {
    12.             _image.sprite = _coinSprites[1];
    13.         }
    14.         else if (curValue >= 30 && curValue < 75)
    15.         {
    16.             _image.sprite = _coinSprites[2];
    17.         }
    18.         else
    19.         {
    20.             _image.sprite = _coinSprites[3];
    21.         }
    22.     }
    Item Drag Handler Contact:

    Code (CSharp):
    1.     //CONTACT METHODS
    2.     public void ItemContactArt()
    3.     {
    4.         _itemImage.color = _contactColor;
    5.     }
    6.     public void ItemDefaultArt()
    7.     {
    8.         _itemImage.color = Color.white;
    9.     }
    Item Slot (Used for OnTriggerEnter2D):

    Code (CSharp):
    1.             if (other.gameObject.CompareTag("InventorySlot"))
    2.             {
    3.                 InventorySlot targetInvSlot = other.gameObject.GetComponent<InventorySlot>();
    4.  
    5.                 if (targetInvSlot.isOccupied)
    6.                 {
    7.                     _parentItem.ItemContactArt();
    8.                 }
    9.                 else if (!targetInvSlot.isOccupied) //If the inventory slot is empty
    10.                 {
    11.                     //TODO: Add visual effect if blocked
    12.                     _parentItem.inventorySlotsToBeFilled.Add(targetInvSlot);
    13.                     _parentItem.targetInventory = targetInvSlot.parentInventory; //Sets the target InvClass
    14.                     slotCanBePlaced = true;
    15.  
    16.                     if (_isTopSlot) //If it is the top slot set it as where to place the item
    17.                     {
    18.                         _parentItem.targetSlot = targetInvSlot;
    19.                     }
    20.                 }
     
  2. JamieRoss95

    JamieRoss95

    Joined:
    Apr 5, 2016
    Posts:
    28
    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!
     
  3. Tr3gan

    Tr3gan

    Joined:
    Dec 13, 2016
    Posts:
    11