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.
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
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?
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 );
}
}
}
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.
Oh, by the way, your dropbox link gives me a 404 error @EmHuynh
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.
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.