- Home /
Why can't I access the TextMesh component of a GameObject?
Hi All,
I am currently attempting to create a Game Object with a TextMesh component when a user moves their mouse over another Game Object, and have this Text Mesh display information on the Game Object that was moused over. Once the mouse is not over the Game Object, the text mesh is then deleted.
The problem that I am encountering is that when I attempt to edit the text of the newly create TextMesh, it returns this error:
NullReferenceException: Object reference not set to an instance of an object StarGUI.OnMouseEnter () (at Assets/StarGUI.js:31) UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32, Int32)
Edit! I've discovered that this only occurs with most of the Game Objects that this StarGUI script is attached to (the stars, which the text mesh is supposed to show information on). I am completely unsure as to why it occurs with most stars, but with a few it works fine..
And here is all of the code in the "StarGUI" file referenced:
#pragma strict
var guiBox : GameObject;
var guiText1 : GameObject;
var guiTextBase : GameObject;
var systemData : GameObject;
var systemProperties : SystemProperties;
var thisStar : GameObject;
var thisStarSystem : StarSystem;
function Start ()
{
Debug.Log("HEY");
systemData = GameObject.Find("SystemData");
systemProperties = systemData.GetComponent("SystemProperties");
thisStar = gameObject;
}
function OnMouseEnter()
{
Debug.Log("OMGIMWORKING");
guiBox = GameObject.CreatePrimitive(PrimitiveType.Cube);
guiBox.name = "GUIBOX" + thisStar.name;
guiBox.transform.position = Vector3(thisStar.transform.position.x + 3 * transform.localScale.x,thisStar.transform.position.y,transform.position.z);
guiBox.transform.localScale = Vector3(thisStar.transform.localScale.x * 3,thisStar.transform.localScale.y * 4,thisStar.transform.localScale.z);
guiText1 = Instantiate(guiTextBase);
guiText1.name = "GUITEXTTHING";
guiText1.GetComponent(TextMesh).text = thisStarSystem.name + "\nPart of " + thisStarSystem.group.name + "\n" + thisStarSystem.numPlanets + " Planets";
guiText1.transform.position = guiBox.transform.position;
guiText1.transform.localScale = Vector3(guiBox.transform.localScale.x/10,guiBox.transform.localScale.y/15,0.1);
}
function OnMouseExit()
{
Destroy(guiText1);
Destroy(guiBox);
}
function Update ()
{
}
Does anyone know what the problem is, or at least the right direction to go in to solve it?
Thanks!
Answer by Jeff-Kesselman · Jun 19, 2014 at 10:49 PM
Line 31 has a LOT of references. You need to determine what the problem really is. First break it up like this:
function OnMouseEnter()
{
Debug.Log("OMGIMWORKING");
guiBox = GameObject.CreatePrimitive(PrimitiveType.Cube);
guiBox.name = "GUIBOX" + thisStar.name;
guiBox.transform.position = Vector3(thisStar.transform.position.x + 3 * transform.localScale.x,thisStar.transform.position.y,transform.position.z);
guiBox.transform.localScale = Vector3(thisStar.transform.localScale.x * 3,thisStar.transform.localScale.y * 4,thisStar.transform.localScale.z);
guiText1 = Instantiate(guiTextBase);
guiText1.name = "GUITEXTTHING";
guiText1.GetComponent(TextMesh).text =
thisStarSystem.name + "\nPart of " +
thisStarSystem.group.name + "\n" +
thisStarSystem.numPlanets + " Planets";
guiText1.transform.position = guiBox.transform.position;
guiText1.transform.localScale = Vector3(guiBox.transform.localScale.x/10,guiBox.transform.localScale.y/15,0.1);
}
Then run and see what line the error is on. Thsi will tell you whether the problem is really thisStarSystem,thisStarSystem.group or guiText1.GetComponent(TextMesh)
If its still on line 31 then insert a Debug.Log() just before it to print the value of guiText1 to make sure that thsi isnt what is null.
Finally, print guiText1.GetComponent(TextMesh) and see if it is returning null.
Once you actually know what is null, then we can help you figure out why...
Thank you; I've followed your advice, and I have found that all the other references, including guiText1, do not return null; but interestingly enough, neither does calling the Text$$anonymous$$esh by Debug.Log(guiText1.GetComponent(Text$$anonymous$$esh)). What I see from this Debug.Log is this: GUITEXTTHING (UnityEngine.Text$$anonymous$$esh) UnityEngine.Debug:Log(Object) StarGUI:On$$anonymous$$ouseEnter() (at Assets/StarGUI.js:32) UnityEngine.Send$$anonymous$$ouseEvents:DoSend$$anonymous$$ouseEvents(Int32, Int32) Stranger still, I still get this error: NullReferenceException: Object reference not set to an instance of an object StarGUI.On$$anonymous$$ouseEnter () (at Assets/StarGUI.js:34) UnityEngine.Send$$anonymous$$ouseEvents:DoSend$$anonymous$$ouseEvents(Int32, Int32)
And yet I do not get it when I call Debug.Log(guiText1.GetComponent(Text$$anonymous$$esh).text);
I still am baffled as to what the problem is. Do you know what it could be?
Thanks
Your answer
