Like the title, In my game, I make a 3D Text that display every time I hit the button, and each time it has a random color. I create an array to store the color
private Color[] textColor = {Color.black, Color.blue, Color.cyan, Color.gray, Color.green, Color.grey, Color.magenta, Color.red, Color.white, Color.yellow};
Then I used this code, when the game’s played, I look into the Inspector window and font material has changed exactly what I need but the color of the 3D text on the screen only changed into 3 types: Red (default color of text), Black, Brown. How can I fix it ?
void Start(){
// some stuffs
textLR = transform.GetComponent<TextMesh>();
}
void Update(){
// some stuffs
index = Random.Range (0, textColor.Length);
}
void OnGUI(){
// some stuffs
transform.GetComponent<MeshRenderer>().material.SetColor("_Color", textColor[index]);
}
This is C# i assume, Im a Js guy but i got it working in both. the only things i changed to your code was making a variable called index and commenting out the textLR line and it works just fine. this is code i put in a C# script called “Test2” on the 3d text:
using UnityEngine;
using System.Collections;
public class Test2 : MonoBehaviour {
private Color[] textColor = {Color.black, Color.blue, Color.cyan, Color.gray, Color.green, Color.grey, Color.magenta, Color.red, Color.white, Color.yellow};
private int index;
void Start(){
// some stuffs
//textLR = transform.GetComponent<TextMesh>();
}
void Update(){
// some stuffs
index = Random.Range (0, textColor.Length);
}
void OnGUI(){
// some stuffs
transform.GetComponent<MeshRenderer>().material.SetColor("_Color", textColor[index]);
}
}
and this is the java script code if you want it:
var textColor = [Color.black, Color.blue, Color.cyan, Color.gray, Color.green, Color.grey, Color.magenta, Color.red, Color.white, Color.yellow];
var index : int;
function Start () {
}
function Update ()
{
index = Random.Range (0, textColor.Length);
transform.GetComponent(MeshRenderer).material.SetColor("_Color", textColor[index]);
}
Hope this Helps!
Your using a TextMesh, correct?
Will this work for you?
private Color[] textColor = new Color[9]{Color.black, Color.blue, Color.cyan, Color.gray, Color.green, Color.grey, Color.magenta, Color.red, Color.white, Color.yellow};
void Start(){
textLR = transform.GetComponent<TextMesh>();
}
void Update(){
index = Random.Range (0, textColor.Length);
}
void OnGUI()
{
textLR.color = textColor[index];
}
Am I misunderstanding something?
There is no need to access the material. TextMesh has a built in ‘color’ property which is public get and set