6
\$\begingroup\$

There are a few objects in the scene as well as a title. I want the title to change its text and color based on the object that is mouse-overed. The script below is added to each object that is intended to change the color and text of the title.

The title changes it's color correctly when values like Color.red are assigned but doesn't change color when values like new Color(254f, 152f, 203f).

public Text title;

void OnMouseEnter()
{
    switch(name)
    {
    CONDITION1:
        title.color = Color.yellow;  // works well
        break;

    CONDITION2:
        title.color = new Color(254f, 152f, 203f);  // doesn't work
        break;
    }

    title.text = name;
}

void OnMouseExit()
{
    title.text = "DEFAULT VALUE";
    title.color = Color.white;
}

Is there a specific reason to the former method to work and latter not to work? How can I change the color of the Text element with custom values?

\$\endgroup\$
5
\$\begingroup\$

Constructor of Color class takes float parameters from 0 to 1 like this title.color = new Color(1f, 0.5f, 0.8f);

\$\endgroup\$
  • \$\begingroup\$ oh god... I don't know the reason behind me assuming the constructor took float values ranged from 0 to 255. It doesn't even make sense! I think I am too tired at this moment:) \$\endgroup\$ – Varaquilex Jan 13 '15 at 8:35
8
\$\begingroup\$

You can do this with the code below:

title.color = new Color(254.0f/255.0f, 152.0f/255.0f, 203.0f/255.0f);
\$\endgroup\$
  • 3
    \$\begingroup\$ This should be accepted as correct answer. \$\endgroup\$ – Eadel Sep 6 '17 at 19:13
2
\$\begingroup\$

If you'd like to pass RGBA in the common way you can also use Color32 that takes 4 bytes ranged from 0 - 255:

title.color = new Color32(254, 152, 203, 255);
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.