Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question UI Text Color change using OnPointerEnter/Exit

Discussion in 'UGUI & TextMesh Pro' started by Vazili_KZ, Aug 19, 2020.

  1. Vazili_KZ

    Vazili_KZ

    Joined:
    Sep 18, 2016
    Posts:
    25
    Hello,

    I'm trying to change the color of a Text UI Element on Mouse Hover, i'm using OnPointerEnter & OnPointerExit to set the color to white on enter and grey on exit. this is a very simple code i wrote just to demonstrate :

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.EventSystems;
    3. using UnityEngine.UI;
    4.  
    5. public class ButtonTest : MonoBehaviour,IPointerEnterHandler, IPointerExitHandler
    6. {
    7.     [SerializeField] private Text _text;
    8.  
    9.     public void OnPointerEnter(PointerEventData eventData)
    10.     {
    11.         Debug.Log("Enter");
    12.         _text.color = new Color(255,255,255,255);
    13.     }
    14.  
    15.     public void OnPointerExit(PointerEventData eventData)
    16.     {
    17.         Debug.Log("Exit");
    18.         _text.color = new Color(100,100,100,255);
    19.     }
    20. }
    21.  
    This code is attached to a button that is the parent of this Text
    when i hover over the Text the color changes correctly but when i exit the hover the color doesn't change to grey and stays white.

    I can't seem to figure out what's causing this issue is there something i'm missing here?
    Thank you guys for your help.
     
  2. JuliaP_Unity

    JuliaP_Unity

    Unity Technologies

    Joined:
    Mar 26, 2020
    Posts:
    666
    Hey, I tried it out and you're right that with that code it didn't work, but when I replaced the "new Color(100,100,100,255)" by, for example, "Color.black" or "Color.blue" it did work, so I guess you just need to figure out a value for your Color that you're happy with but overall what you're trying to do should work. I tested it on Unity 2020.1.0f1.
     
  3. Vazili_KZ

    Vazili_KZ

    Joined:
    Sep 18, 2016
    Posts:
    25
    Hello,thank you for your answer,
    Indeed it does work that way, but one should be able to use any color. I have found a workaround that involves resetting the color on exit before setting the new one. I'll post my code later during the day.
    Thanks again:)
     
  4. Vazili_KZ

    Vazili_KZ

    Joined:
    Sep 18, 2016
    Posts:
    25
    So OnPointerExit i call this method :

    Code (CSharp):
    1. private void ResetTabs()
    2.     {
    3.         foreach (TabButton tabButton in tabButtons)
    4.         {
    5.             if (_selectedTab!=null && tabButton == _selectedTab) continue; // dont reset the currently selected tab
    6.             tabButton._buttonImage.color = imageIdleColor;
    7.             tabButton._buttonText.color = textIdleColor;
    8.         }
    9.     }
    and at the beginning of each Event (Enter/Click) i fist call ResetTabs() then execute the piece of code that i want and this seems to be working perfectly.
    Hope this helps someone who faces the same issue :)
     
    JuliaP_Unity likes this.
  5. OinkleMyCow

    OinkleMyCow

    Joined:
    Apr 2, 2021
    Posts:
    1
    Hello! I found a resolution to this as I was having the same issue. A fix is to change the new Color(); to new Color32(); which is able to use values from 0 to 255 while Color is 0.1f to 1f. Hope this works!