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!
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
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).
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.