Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by AndrewVanB · Nov 20, 2016 at 11:06 PM · c#text

Text Colour not changing?

I'm trying to have randomly set float values change the colour of my text, but I can't seem to make it work, as far as I can tell this chunk of code should work just fine.

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class TextColorChanger : MonoBehaviour {
 
     public Text[] Change;
 
     void Start() {
         RainbowColour();
     }
 
     void RainbowColour() {
             float rCol, gCol, bCol;
 
             rCol = Random.Range(0, 255);
             gCol = Random.Range(0, 255);
             bCol = Random.Range(0, 255);
 
             Change[0].color = new Color(rCol, gCol, bCol);
             Change[1].color = new Color(rCol, gCol, bCol);
 
             Debug.Log("rCol: " + rCol + " gCol: " + gCol + " bCol: " + bCol);
     }
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by fmtrick · Nov 21, 2016 at 07:21 AM

set those Col variables to integers since you're using whole numbers. That may help.

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
avatar image
0

Answer by brunocoimbra · Nov 21, 2016 at 12:11 PM

Your problem is with "while (true)"! If you want it to be updated, then use the Update method, the ONLY way to use "while (true)" in Unity is in a Coroutine.

  using UnityEngine;
  using UnityEngine.UI;
  using System.Collections;
  
  public class TextColorChanger : MonoBehaviour {
  
      public Text[] Change;
  
      void Start() {
          StartCoroutine(RainbowColour());
      }
  
      IEnumerator RainbowColour() {
          while (true) {
              float rCol, gCol, bCol;
  
              rCol = Random.Range(0, 255);
              gCol = Random.Range(0, 255);
              bCol = Random.Range(0, 255);
  
              Change[0].color = new Color(rCol, gCol, bCol);
              Change[1].color = new Color(rCol, gCol, bCol);
  
              Debug.Log("rCol: " + rCol + " gCol: " + gCol + " bCol: " + bCol);
 
              yield return new WaitForSeconds(0.5f);
          }
      }
  }

Here is a little explanation about how to use Coroutines:

  1. Coroutines are like a method, but that follows a routine (!), executing piece by piece after meeting the desired condition;

  2. It should be started with "StartCoroutine(MyCoroutine());";

  3. The coroutine should be a Method with IEnumerator as it's return (not "void");

  4. Inside it, you should use "yield return" followed by the condition that it should wait to continue it's execution (in this case, we say that we want to wait half a second). If you use "yield return null;" if will just wait pass 1 frame. To stop the Coroutine execution you could use "yield break;"

If you would like to undestand more about how to use coroutines, take a look around, there is a plenty of answer already about it.

Comment
Add comment · Show 4 · 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 AndrewVanB · Nov 21, 2016 at 03:17 PM 0
Share

The while statement wasn't actually supposed to be there, forgot to remove it from the question, with it removed the colour should change once, but it remains white, and all the text colour values stay at 255.

avatar image brunocoimbra AndrewVanB · Nov 21, 2016 at 03:33 PM 0
Share

About that, Color(r, g, b) takes arguments from 0 to 1. To use the 255 format, use Color32 ins$$anonymous$$d.

avatar image AndrewVanB brunocoimbra · Nov 21, 2016 at 04:17 PM 0
Share

I still seem to be having issues, at the point I just want to assume I'm going crazy and missing something really simple, the Color32 will change constantly, but the text will stay un-affected.

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class TextColorChanger : $$anonymous$$onoBehaviour {
 
     public Text[] Change;
     public Color32 textColor32;
 
     void Awake() {
         Change[0] = this.GetComponent<Text>();
     }
 
     void Update() {
         RainbowColour();
     }
 
     void RainbowColour() {
 
         textColor32 = new Color32((byte)Random.Range(0, 255), (byte)Random.Range(0, 255), (byte)Random.Range(0, 255), 1);
 
         Change[0].color = textColor32;
     }
 }
Show more comments

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

263 People are following this question.

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

Related Questions

how do i reference to a text objects text in scripting? 2 Answers

How To Load TTF Font From External File 0 Answers

How to link up instantiated text in list to allow buttons to adjust number shown, c# 1 Answer

Removing an item from Inventory and updating item count on HUD 0 Answers

How to print variable? (C#) 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