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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to set text to a colour that is in a RGBA, Hex or other type of form?

Discussion in 'Editor & General Support' started by MattCarter24, Jun 27, 2015.

  1. MattCarter24

    MattCarter24

    Joined:
    May 27, 2015
    Posts:
    120
    Hello,

    I have code that changes a UI text and it works perfectly.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class Finish : MonoBehaviour {
    6.  
    7.     string myText;
    8.     public int score = 0;
    9.  
    10.     public Text Status;
    11.    
    12.     GameObject myTextgameObject; // gameObject in Hierarchy
    13.     Text ourComponent;           // Our refference to text component
    14.  
    15.     // Use this for initialization
    16.     void Start () {
    17.         if (PlayerPrefs.HasKey("Score") == true)
    18.         {
    19.             score = PlayerPrefs.GetInt("Score");
    20.         }
    21.        
    22.         // Find gameObject with name "MyText"
    23.         myTextgameObject = GameObject.Find("MyText");
    24.         // Get component Text from that gameObject
    25.         ourComponent = myTextgameObject.GetComponent<Text>();
    26.  
    27.         if(score == 7 || score == 8){
    28.             Status.text = "Excellent!";
    29.             Status.color = Color.green;
    30.         }
    31.         if(score == 5 || score == 6){
    32.             Status.text = "Pretty Good!";
    33.             Status.color = Color.green;
    34.         }
    35.         if(score == 3 || score == 4){
    36.             Status.text = "Okay!";
    37.             Status.color = Color.yellow;
    38.         }
    39.         if(score == 0 || score == 1 || score == 2){
    40.             Status.text = "Better luck next time!";
    41.             Status.color = Color.red;
    42.         }
    43.     }
    44.    
    45.     // Update is called once per frame
    46.     void Update () {
    47.         ourComponent.text = score.ToString();
    48.     }
    49. }
    50.  
    The problem is that I'm only restricted to using the words red, yellow, blue, green, black, cyan etc. But for example, I want range but that word doesn't exist in the Unity editor. Well... I would like to have an advanced colour. Can I somehow create a new colour with a Hex code or a RGBA code or anything else like that?.
     
  2. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,986
  3. MattCarter24

    MattCarter24

    Joined:
    May 27, 2015
    Posts:
    120
    It didn't work

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class Finish : MonoBehaviour {
    6.  
    7.     public Color eight;
    8.  
    9. void start(){
    10.  
    11.         eight[0] = 0;
    12.         eight[1] = 255;
    13.         eight[2] = 0;
    14.         eight[3] = 1;
    15.  
    16. }
    17. }
     
  4. MattCarter24

    MattCarter24

    Joined:
    May 27, 2015
    Posts:
    120
    The code above is me trying to access the RGBA components
     
  5. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,986