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

TextMeshProUGUI not changing color after instantiated within prefab.

Discussion in 'Getting Started' started by unity_edOjmOMsyZChOA, Aug 16, 2022.

  1. unity_edOjmOMsyZChOA

    unity_edOjmOMsyZChOA

    Joined:
    Sep 22, 2020
    Posts:
    7
    Hello guys.

    I'm trying to instantiate a variable amount of prefabs with the TextMeshProUGUI component attached to it. But whenever i try to change the text color of the TextMeshProUGUI component nothing happens, it always stays black. I have tried setting the color via tags/faceColor with Color/Color32 with no success.

    This is the code i used to instantiating the Objects. It self is sitting on the canvas:
    Code (CSharp):
    1. public class PhaseDisplay : MonoBehaviour
    2. {
    3.     [SerializeField] private GameObject phaseDisplayPrefab;
    4.  
    5.     [SerializeField] private Color32 onColor;
    6.     [SerializeField] private Color32 offColor;
    7.  
    8.     private TextMeshProUGUI[] displays;
    9.  
    10.     private void Awake()
    11.     {
    12.         GameEvents.current.onChangePhase += UpdatePhaseDisplay;
    13.     }
    14.  
    15.     private void OnDestroy()
    16.     {
    17.         GameEvents.current.onChangePhase -= UpdatePhaseDisplay;
    18.     }
    19.  
    20.     private void Start()
    21.     {
    22.         SetUpPhaseDisplay();
    23.     }
    24.  
    25.     private void SetUpPhaseDisplay()
    26.     {
    27.         TurnPhase[] phaseOrder = GameController.current.GetPhaseOrder();
    28.         displays = new TextMeshProUGUI[phaseOrder.Length];
    29.  
    30.         for (int i = 0; i < phaseOrder.Length; i++)
    31.         {
    32.             TextMeshProUGUI display = Instantiate(phaseDisplayPrefab, transform).GetComponent<TextMeshProUGUI>();
    33.             displays[i] = display;
    34.             TurnPhase curPhase = phaseOrder[i];
    35.             display.text = curPhase.ToString();
    36.             display.color = offColor;
    37.         }
    38.     }
    39.  
    40.     public void UpdatePhaseDisplay(TurnPhase phase)
    41.     {
    42.         for (int i = 0; i < displays.Length; i++)
    43.         {
    44.             TextMeshProUGUI display = displays[(int)phase];
    45.  
    46.             if (i == (int)phase)
    47.             {
    48.                 display.color = new Color(0, 0, 0, 0);
    49.             }
    50.             else
    51.             {
    52.                 display.color = offColor;
    53.             }
    54.         }
    55.     }
    56. }
     
  2. unity_edOjmOMsyZChOA

    unity_edOjmOMsyZChOA

    Joined:
    Sep 22, 2020
    Posts:
    7
    I figured it out after sleeping over it. My problem was that I updated the color of the wrong Component. Line 44 should be:
    Code (CSharp):
    1. TextMeshProUGUI display = displays[i];