obsssve_cmplsve
obsssve_cmplsve

Reputation: 17

Public variable working in Inspector, but not being displayed in the Scene or Game windows

In my game I have one countdown script. Once its timer (3 seconds) reaches zero, gameplay starts.
I wanted to make it so that the text will change colour during countdown (i.e. 3 - red, 2 - yellow, 1 - green).

This was rather easy to achieve, but I wasn't entirely happy with using countdown.color = Color.green; and wanted more flexibility (i.e. having a colour picker in the Editor).

This is the code that works:

Text countdown;

void OnEnable()
{
    countdown = GetComponent<Text>();
    countdown.text = "3";
    StartCoroutine("Countdown");
}

IEnumerator Countdown()
{
    int count = 3;

    for (int i = 0; i < count; i++)
    {
        countdown.text = (count - i).ToString();
        if ((count - i) == 3)
        {
            countdown.color = Color.red;
        }
        if ((count - i) == 2)
        {
            countdown.color = Color.yellow;
        }
        if ((count - i) == 1)
        {
            countdown.color = Color.green;
        }
        yield return new WaitForSeconds(1);            
    }
    StartRound();        
}        

And this is the one that's giving me headaches:

public Color color3;
public Color color2;
public Color color1;

Text countdown;

void OnEnable()
{
    countdown = GetComponent<Text>();
    countdown.text = "3";
    StartCoroutine("Countdown");
}

IEnumerator Countdown()
{
    int count = 3;

    for (int i = 0; i < count; i++)
    {
        countdown.text = (count - i).ToString();
        yield return new WaitForSeconds(1);

        if ((count - i) == 3)
        {
            countdown.color = color3;                
        }

        if ((count - i) == 2)
        {
            countdown.color = color2;                
        }

        if ((count - i) == 1)
        {
            countdown.color = color1;                
        }        
    }
    StartRound();        
}        

The 2nd script works, but only up to a point:
- the 3 colours show up in the Inspector, and I can open the colour picker and select my desired colours;
- once I click Play though, the text doesn't show up at all (the countdown still works and gameplay starts normally after those 3 seconds), as if alpha is set to 0;
- during the countdown I can see the default colour picker (the one in the Text (Script) component) cycling through my 3 colours in the Inspector, yet I'm getting nothing in my Scene or Game view.

Any help would be greatly appreciated. Thank you in advance.

Upvotes: 1

Views: 73

Answers (1)

Foggzie
Foggzie

Reputation: 9821

the text doesn't show up at all ... as if alpha is set to 0

Your alpha is very likely set to 0; your code looks fine and if the first script works, then replacing it with the second script should work with correctly configured colors (hence my assumption). If you take a look at this screenshot of the color picker, you'll notice that the color shows up in the inspector and in the color wheel but the A value (below R, G, and B) is 0.

Color Picker in Inspector with 0 alpha but color still shows

You have to manually set it to max (or whatever non-0 value you'd like) and that will be represented in the inspector by a white bar filling up below the representation of the color. Notice in this next screenshot, I've got it set to about half so there's half of a white bar below the color in the inspector:

enter image description here

Upvotes: 1

Related Questions