MeshText - Object reference not se to an instance of an object

Hi there,

i got some method, which i am calling after pressing a button:

void Test()
{
TextMesh tm;
tm = gameObject.GetComponent();
tm.text = “text”;
tm.fontSize = 20;
tm.transform.localPosition = new Vector3(0.0f,0.5f,0f);
Debug.Log(tm.text);
}

But it yells at me: Object reference not se to an instance of an object. I read some tutorials on forums and unity docs, but cant help myself.

Thx a lot, if some good soul help me :slight_smile:

Does your game object that this script is attached to have a TextMesh component? Can you show the full error details? You can copy from the console and paste it here.

NullReferenceException: Object reference not set to an instance of an object
GraphBehaviourClass.Test () (at Assets/Scripts/GraphBehaviourClass.cs:94)
GraphBehaviourClass.menuWindow (Int32 ID) (at Assets/Scripts/GraphBehaviourClass.cs:60)
UnityEngine.GUI.CallWindowDelegate (UnityEngine.WindowFunction func, Int32 id, UnityEngine.GUISkin _skin, Int32 forceRect, Single width, Single height, UnityEngine.GUIStyle style) (at C:/BuildAgent/work/7535de4ca26c26ac/Runtime/ExportGenerated/Editor/GUI.cs:1283)

I have no gameobject, I just used syntax from websites hoping that is some kind of unity thing :))

Any objects that exist in the scene is a game object (or Transform, whichever way you wanna see it but I won’t delve into it). The game object that the script you have attached to requires a TextMesh component because you are calling GetComponent.

You are probably getting a null exception because GetComponent has failed to get the component (because there isn’t one) and so it is returning null. And because you are trying to excess a null object, the error comes up.

Yeah, I got that, but how to create MeshText without gameObject? I just wanna create some labels in 3D around my graph component.

When you create a TextMesh, it will already come with a game object. All you have to do is to attach the script to the TextMesh.
GameObject → Create Other → 3D Text

Oh yeah, I thought I´ll create one in the code. I´ll try, thx. Hope it´ll work.

Oh you wanted to create one from code? In that case you’ll want to use AddComponent instead. AddComponent will add the component while GetComponent will get the component.

void Test()
{
TextMesh tm;
tm = gameObject.AddComponent<TextMesh>();
tm.text = "text";
tm.fontSize = 20;
tm.transform.localPosition = new Vector3(0.0f,0.5f,0f);
Debug.Log(tm.text);	
}

More information here: Unity - Scripting API: GameObject.AddComponent

Yeah, that´s what I was looking for. I´ll try then.

It seems working. Just need to calculate the position of the labels. Thx a lot :slight_smile: (You know, in Csharp U just pust the key word new :-)) )

//tm is TextMesh
tm.text = “TEXT”;
tm.fontSize = 2000;
tm.offsetZ = 5;
//this works fine with on gameObject, but with tm not
tm.renderer.material.SetColor(“_Color”, Color.red);
//Unity allows me this, but I think it has no effect, so how I can place the TextMeshes?
tm.transform.localPosition = new Vector3(0f, 0f, 0f);

→ I have no labels (TextMeshes) in Game Scene, but Debug.Log shows me their text…