- Home /
I cannot change 3D Text (Text Mesh) color
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]);
}
Hey BraveVN, I answered this the other day for someone else on here, below is the link and please use the search functionality of the site , it may turn up more than you expect.
Heres the link
Cheers bud Gruffy Edit: btw, read the whole thing, at the bottom of answer there is a zip file to download if you find following the resolution difficult or anything.
thank you, I'll try this and come here to response later, it look hard for me :)
Answer by akauper · Apr 14, 2014 at 12:51 AM
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
Answer by importsjc · Apr 12, 2014 at 02:41 PM
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!
Thanks for your help, but it's not work, just red (default color), brown and black :(
Just tested the code, it seems to work fine for me. Try making a new project, then GameObject>create other>3D Text, create a new javascript and call it "Test", paste this code into the test script:
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($$anonymous$$eshRenderer).material.SetColor("_Color", textColor[index]);
}
and finally attach the script to your 3d text object. i did this and it works perfectly. Good Luck!
It's worked, thanks, may be the error is in another places of my project