Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by bking20 · Sep 18, 2016 at 02:11 PM · textcolor change

Changing text color at runtime always white...

I'm trying to change text to a specific shade by using this code:

 timeColor = new Color (68f, 144f, 233f, 1f);
 timerDisplay.gameObject.GetComponent<Text> ().color = timeColor;

And have also tried this:

 timeColor = new Color (68f, 144f, 233f, 1f);
 timerDisplay.color = timeColor
 

They both turn my text to white for some reason. Any ideas why?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
6
Best Answer

Answer by flashframe · Sep 18, 2016 at 02:13 PM

The RGB values of a Color need to be float values between 0-1. (You can calculate these values by dividing by 255.)

If you want to use byte values, you need to use Color32 https://docs.unity3d.com/ScriptReference/Color32.html

 timeColor = new Color32(68, 144, 233, 255);



Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image bking20 · Sep 18, 2016 at 06:30 PM 0
Share

Thanks, worked perfectly!

avatar image
0

Answer by Sinnii · Sep 18, 2016 at 02:15 PM

color is not a property of the text itself, but of the UI element that contains it. What you want is timerDisplay.font.material.color

And yeah, as the above answer says you need 0-1 values.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image bking20 · Sep 18, 2016 at 06:08 PM 0
Share

Thanks guys, that makes sense. So do I need to add a material to the font? Because even with these changes, my font is not changing (in this case to red).

                 if (secs >= 0) {
             if (secs <= 10) {
                 timeColor = new Color32 (255, 70, 57, 255);
                 Debug.Log ("red");
                 if (interval > tics && tics > 0) {
                     source.PlayOneShot (alarm);
                     interval--;
                 }
             } else {
                 timeColor = new Color32 (66, 154, 255, 255);
                 Debug.Log ("blue");
             }
             //timerDisplay.color = new Color(68f, 144f, 233f, 1f);
             timerDisplay.font.material.color = timeColor;
             timerDisplay.text = "Time: " + secs;
         }
avatar image
0

Answer by codemaker2015 · Sep 23, 2021 at 01:05 PM

You can use color attribute to provide color for the text.

It can be done by two ways -> using the color static attributes and color class constructor.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class ColorDemo : MonoBehaviour {
 
     [SerializeField]
     Text infoText;
 
     void Start () {
        infoText.text = "Hello World";
        infoText.color = Color.red; //Red color using Color class
        //Red color using hex value. (255,0,0) => 255/255, 0/255, 0/255) => (1,0,0)
        infoText.color = new Color(1f, 0f, 0f); 
        // Color with opacity value. 1 means opaque and 0 means transparent.
        infoText.color = new Color(1f, 0f, 0f, 0.5f); 
     }
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to access a color variable from a different class 0 Answers

Text - Negative Color from Background 0 Answers

Please how to change the color of object and the toolTip based on Text file? 0 Answers

creating a private float variable that shows the amount of time the text 0 Answers

Gui text script - help 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges