Change Text Color on Hover (Help)

Alright so basically what I want this script to do is to change the text color on hover for a 3D text and it’s not working is their anything wrong?

function OnMouseEnter()
{
    renderer.material.color = Color.red;
}

function OnMouseExit()
{
    renderer.material.color = Color.white;
}

I actually am trying this with a GUI Text and it’s still not working it says I need a renderer I don’t know what I have to apply to get that to work, any tips? Thanks!

If you are using GUIText, you can get the color from the GUIText. http://docs.unity3d.com/Documentation/ScriptReference/GUIText-color.html
If you are using 3D text (TextMesh), did you attach the script to the TextMesh?

I still don’t quite understand, sorry. I want to use a GUIText but how do I get it so OnMouseEnter it changes from white to red and when I leave it goes back to white. I am using Javascript.

Instead of using renderer.material.color to change the color of a GUIText, you should use the GUIText component’s color.

// Example code
var myGUIText : GUIText;

function OnMouseEnter()
{
    myGUIText.color = Color.Red;
}

function OnMouseExit()
{
   myGUIText.color = Color.White;
}

If you are using 3D text, which uses a TextMesh component, it would require a renderer component as well. Your code only works for the 3D Text but it’ll only work if you attach it to the 3D Text game object with the TextMesh and Renderer component because of how you write your script.

renderer.material.color = Color.Red; // Is the same as below
this.renderer.material.color = Color.Red; // Is the same as above. 
// You are referencing the renderer component from this game object that this script is attached to.

var myTextMesh : GameObject;
myTextMesh.renderer.material.color = Color.Red; // You are referencing the renderer component from the game object, myTextMesh

When I use this script

var myGUIText : GUIText;

function OnMouseEnter()
{
    myGUIText.color = Color.Red;
}

function OnMouseExit()
{
   myGUIText.color = Color.White;
}

It says “NullReferenceException: Object reference not set to the instance of an object” Thanks for the help but I don’t know what is going on and what to do!

You need to assign myGUIText variable in the inspector. If it is not assigned, myGUIText is null and it cannot change the color of a null object (because there’s no object to change the color of).

I assigned the variable in the inspector to the GUIText and it still says that.

Have you tried setting the part empty and re-assigning? Otherwise, have you checked if your game object has a GUIText component attached to it (it won’t let you assign it in the inspector but just check anyway).

If you are absolutely sure you have assigned the correct game object and tried re-assigning, try restarting Unity. Sometimes I get this problem when I’ve assigned the correct objects. This problem happens quite rarely though, like when the pig flies over the blue moon kind of rare.

EDIT
After seeing your image, that null error isn’t from the earlier script right?

No it’s still not working I deleted the GUI Text readded it assigned the script and put the myGUIText and assigned it to the GUIText in the heirarchy and it still says the null thing.

It’s this script

var myGUIText : GUIText;

function OnMouseEnter()
{
    myGUIText.color = Color.Red;
}

function OnMouseExit()
{
   myGUIText.color = Color.White;
}

Here, try this instead. Rather than using Color, try getting from the Material instead (you need to create one btw)

#pragma strict

var myGUIText : GUIText;

function OnMouseEnter()
{
    myGUIText.material.color = Color.red;
}

function OnMouseExit()
{
   myGUIText.material.color = Color.white;
}

Thanks it worked perfectly! Sorry for me being retarded and not understanding quite well. Taking C++ classes and Javascript courses next year haha.