0

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.

derHugo
  • 83,094
  • 9
  • 75
  • 115
  • Please use the correct tags! Note that [`[unityscript]`](https://stackoverflow.com/tags/unityscript/info) is or better **was** a custom JavaScript flavor-like language used in early Unity versions and is **long deprecated** by now. – derHugo Aug 25 '22 at 14:22

3 Answers3

2

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.

1

Awake is executed before Start. You can put everything in Awake.

pixlhero
  • 418
  • 2
  • 8
  • True but `Awake` has other purposes beyond that of `Start` such as _"Instead, you should use Awake to set up references between scripts, and use Start, which is called after all Awake calls are finished, to pass any information back and forth"_ which is kinda nice but not needed here. `Awake` is also called even if the corresponding game component is not enabled. In this case `Start` is arguably all that is needed. https://docs.unity3d.com/ScriptReference/MonoBehaviour.Awake.html –  Jul 22 '22 at 23:00
0

Thank you all, i have put it to Awake so far and it worked wonderfully, also changed color of my text.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 27 '22 at 00:34