I’m trying to change the color of a text when a button is selected.
I’ve declared a serialized private Color var (to change it in Unity) and tried to change it through script as below:
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class changeColorOnClick : MonoBehaviour, ISelectHandler
{
public Text text;
public Button button;
[SerializeField] private Color textColor;
// Start is called before the first frame update
void Start()
{
button = this.GetComponent<Button>();
//button.OnSelect(TextOnSelect);
}
void ISelectHandler.OnSelect(BaseEventData eventData)
{
text.color = textColor;
}
}
When I look at the inspector, when the button is selected the text color changes, but the text itself disappears.
However, if I change that line as follows it simply works