- Home /
3D Text - How to change colour - Doesn't seem to work
Okay so my script is below, and if I view it on my font materials the colour changes, however it doesn't change when on the actual game view? - very odd, anyone got any input on how I can fix this? - I'm doing a very basic Main Menu.
var isQuit=false;
function OnMouseEnter(){
//change text color
renderer.material.color = Color.red;
}
function OnMouseExit(){
//change text color
renderer.material.color = Color.black;
}
function OnMouseUp(){
//is this quit
if (isQuit==true) {
//quit the game
Application.Quit();
}
else {
//load level
Application.LoadLevel("level1");
}
}
function Update(){
//quit game if escape key is pressed
if (Input.GetKey(KeyCode.Escape)) { Application.Quit();
}
}
Answer by edcarlo · Jan 07, 2014 at 06:21 AM
// try this
gameObject.GetComponent<TextMesh>().color = Color.red;
Thanks for the reply, tried that but getting the following error. "Assets/Scripts/javascript/mainmenu.js(5,35): BCE0043: Unexpected token: )."
I know I might be late, but in case anyone has the problem with an unexpected token error, try to use gameObject.GetComponent(Text$$anonymous$$esh).color = Color.red; At least this works in unity 5.
Answer by Brood · Jan 07, 2014 at 11:00 AM
I am assuming that, in the game view, when you hover the mouse over the 3D text (hence OnMouseEnter), you want it to change to red, and when you leave the mouse from the 3D text (hence OnMouseExit), you want it to change it back to black.
If thats the case, changing color of the text in the game, it is possible that you didn't add a collider to the 3D text. A collider is absolutely necessary for the OnMouseEnter/OnMouseExit to function, as it allows detection of the mouse.
Solution:
Select the 3D text, then go to its inspector.
Add Component - Physics - Box Collider
Select the Box Collider, and you should see a green wireframe on the scene view.
Adjust the collider to fit the text.
If successful, the 3D text should recognise the mouse and should be able to change color.
This is my best suggestion that I can provide, and it's possible that this isn't what you are looking for. If so, sorry about that. My only favor from you is to be specific with your question, as I can also assume that you want to see the change of your font material color in game.
Good day.
Thanks for the reply. Yes you are correct in assu$$anonymous$$g that I wish to see a change in font colour in game.
I've added a box Collider to both my 3D Text objects but it's still not changing, if I look in the Text $$anonymous$$aterial view I can see it changes colour there, but in game view it doesn't.
I know the box collider works because my On$$anonymous$$ouseUp command works for both pieces of text.
I've deleted my old 3D text objects and started them from fresh, the script I used first now seems to work, looks like I might have changed something without realising. All is good now, how do I close this question?
From what I researched, the most I found out it you need 1k points of karma to be able to close a comment. Bet you can get that piece-of-cake?
Your answer
Follow this Question
Related Questions
Switching color of light 1 Answer
Why does my 3d text keep turning white? 1 Answer
change a material from multiple materials 1 Answer
change skybox via script help ? 1 Answer