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 Gamingsaut · Feb 14, 2016 at 02:54 AM · scripting problemscripting beginner

How To change a Text Object's Color Randomly using Color 32?

  I have been trying to change a Text Object's color randomly using Color 32 and a timer. I have the timer down, but I don't quite understand how to change a Text Object's color using Color 32 and random.Range. If you could tell me and leave an example in C Sharp it would be very much appreciated.
 




Comment
Add comment · Show 1
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 Gamingsaut · Feb 14, 2016 at 02:56 AM 0
Share

I almost forgot, Here is a picture of my code: Script Photo

help-code.png (16.3 kB)

2 Replies

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

Answer by EmHuynh · Feb 14, 2016 at 05:18 AM

Hello, @Gamingsaut

Here is a script that I wrote to demonstrate the use of Random.Range to generate a random color for a Color32 variable. A timer wasn't used because of the lack of explanation of how it should be used to affect the randomization of the color. I hope you gain an idea on how you can change the color of a text object randomly. Thanks for asking, if there any more questions, let us know!


Let me know if there are any improvements that I can make within my script, so I can learn from it! Here is the script (http://bit.ly/1QdX0Ke):

 using UnityEngine;
 using System.Collections;
 
 public class RandomizeTextColor32 : MonoBehaviour
 {
     public Color32 textColor32;
 
     void RandomizeTextColor()
     {
         textColor32 = new Color32(
             ( byte )Random.Range( 0, 255 ),        // R
             ( byte )Random.Range( 0, 255 ),        // G
             ( byte )Random.Range( 0, 255 ),        // B
             ( byte )Random.Range( 0, 255 ) );      // A
     }
 
     void Update() 
     {
         if( Input.GetKey( KeyCode.Space ) ) {
             RandomizeTextColor();
             Debug.Log( textColor32 );
         }
     }
 }

Short explanation:

For each of the elements of the textColor32 (r, g, b, a), I called Random.range to generate a random int value between 0 to 255. I then convert each of those values to byte. Here's the URL to the documentation of Random.range: http://docs.unity3d.com/ScriptReference/Random.Range.html

Comment
Add comment · Show 2 · 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 Gamingsaut · Feb 14, 2016 at 07:33 AM 0
Share

Wow! Thanks for the response. It was very informative. I only have one problem. I added the script to my text object, and the Debug.Log showed the color changing. The change in color even showed up in the inspector. However, on the scene and game view, the Text's color did not change. Do you have any idea how I could fix this?Picture 2 Picture1

help-code-1.png (19.1 kB)
avatar image EmHuynh · Feb 14, 2016 at 10:24 AM 0
Share

Hey, @Ga$$anonymous$$gsaut. Glad I could help! To resolve your issue, we first need get to access to the text object. So we have to declare a Text variable. Then, we set textColor32 as its color.

I have updated the script, just in case you or anybody needs a reference. Read the comments to understand how it works!

Here's the script (RandomizeTextColor32.cs): http://bit.ly/1QdX0$$anonymous$$e

 using UnityEngine;
  using System.Collections;
  using UnityEngine.UI;
  
  public class RandomizeTextColor32 : $$anonymous$$onoBehaviour
  {
      public Color32 textColor32;    // The color that will be randomly set and
                                     // applied to [textObject].
      public Text textObject;        // The Text object that we want to edit.
  
      void Awake() {
         // Initialize [textObject] by getting the
         // Text component of object that this script is attached to.
         textObject = this.GetComponent< Text >();
      }
  
      void RandomizeTextColor()
      {
         // Randomly set each values of textColor32 by using Random.Range.
         // Call Random.Range and convert the random int value to byte.
         textColor32 = new Color32(
             ( byte )Random.Range( 0, 255 ),     // R
             ( byte )Random.Range( 0, 255 ),     // G
             ( byte )Random.Range( 0, 255 ),     // B
             ( byte )Random.Range( 0, 255 ) );   // A
          
         // Set the color of [textObject] to [textColor32]
         textObject.color = textColor32;
      }
  
      void Update() 
      {
          if( Input.Get$$anonymous$$ey( $$anonymous$$eyCode.Space ) ) {
              RandomizeTextColor();
              Debug.Log( textColor32 );
          }
      }
  }
avatar image
0

Answer by Gamingsaut · Feb 14, 2016 at 08:47 PM

  Thanks! The script works now. I had thought that just attaching the script component to the Text Object would make it work correctly. Clearly I was wrong. The script works now, thank you for all the help.
Comment
Add comment · Show 5 · 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 EmHuynh · Feb 14, 2016 at 10:02 PM 0
Share

Awesome, I am glad it all worked out!

avatar image Gamingsaut · Feb 14, 2016 at 10:53 PM 1
Share

Oh, by the way, your dropbox link gives me a 404 error @EmHuynh

avatar image Gamingsaut Gamingsaut · Feb 14, 2016 at 11:01 PM 0
Share

Also, I didn't notice this earlier, when I put in the script, the "public" in the class name gives me an red line underneath it, and tells me it was "expecting 'or' ". Do you know why this could be?

It looks like this "public class Color : $$anonymous$$onoBehaviour {" except the word public has a red line underneath it.

avatar image EmHuynh Gamingsaut · Feb 15, 2016 at 01:56 AM 0
Share

Hey, @Ga$$anonymous$$gsaut! Thanks for infor$$anonymous$$g me about the invalid link. Here's a working link: http://bit.ly/1PLBvxT

Did you rename the class I made called, "RandomizeTextColor32" to "Color"? To avoid any confusion, you should give the class a different name because Unity3D have created a struct named Color.

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

45 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

Related Questions

Toggle direction 0 Answers

level manger and starting scene will not work together 0 Answers

Measuring/finding the height of a stack of game objects then recording that number. 2 Answers

Flashlight flickering script? 0 Answers

NavMesh of an automatically generated maze 0 Answers


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