Search code examples
c#unity-game-enginecolorstextmeshpro

Unity - changing colors via script not working?


still learning Unity. I want to change text of a dots under button. These dots are TextMeshPro objects. I'd like to set the color as variable to be able to change it later in the app.

My code is:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class Colors : MonoBehaviour
{

    public TextMeshProUGUI threeDotsText1;
    public TextMeshProUGUI threeDotsText2;
    public TextMeshProUGUI threeDotsText3;

    public Color dotsColor;

    void Start()
    {
        Color dotsColor = new Color(0.1f, 0.5f, 0.15f, 1.0f);
    }

    void Awake()
    {
        Debug.Log(dotsColor);
        //threeDotsText1.color = dotsColor;
        //threeDotsText2.color = dotsColor;
        //threeDotsText3.color = dotsColor;

    }
}

This didn't work any way i tried. That's why i commented last part of a code and left only "Debug.Log(dotsColor);" to see the output in a console...

Output is (and i don't understand why) this:

enter image description here

I have tried googling different ways to change the color, so far failed.

Simple task, don't know why it's not working.


Solution

  • As @pixlhero said, Awake is executed before Start. There is also the problem that you are creating a new local variable in Start, and not assigning the public field.