public class GraphBehaviourClass : MonoBehaviour
{
TextMesh tm;
...
...
private void someMethod()
{
...
tm = gameObject.AddComponent<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… Any help? Thx.
Please use code tags. http://forum.unity3d.com/threads/143875-Using-code-tags-properly
Have you tried taking a look at your game object with the text mesh component? Try focusing to it in the scene view. It might be too small. Check if the camera is looking at it.
I am pretty sure, that it has 2000 font size it should be somewhere. Because other things I can see.
Try running the game, select the gameobject in the hierarchy and press F to focus it in the scene view. See if there’s anything rendering. Else, did you assign the font material to the gameobject with the TextMesh component? Check if your TextMesh component is missing anything.
Naaah, it doesnt show up in Hierarchy, but Debug.Log show me the text of TextMesh… damn. And if I use renderer, it yells at me, that I cant use it. Dont know why, cause with other game objects it works fine.
When you say it yells at you…you mean it throws out an error? On closer inspection, how on earth is your script even able to bypass the compiler? O.o
You closed the class and left your function outside the class. Unity shouldn’t even allow you to run that script.
If your game object did not have a TextMesh component added, Unity would have thrown a null exception when you try to access it.
Not added? I am adding it by AddComponent… and yeah, the closing class earlier its just a typo, no worry 
Okay, when you select the game object with the script attached to it…
- Is there a TextMesh Component?
- Does it have a renderer?
- What is the material on the renderer?
- Is the material on the renderer for text (TextShader)?
- Does your TextMesh have a font assigned to it?
- What is the value of the character size of your font? Try assigning a value for this.
I created TextMesh just in the code, its in class, which is attached to the mainCamera. I tried to set up font material, aint working. I dont know, which properties are obligatory to create Meshtext, maybe this is the problem.
Can you try attaching the script to an empty game object? Try attaching a renderer component to the object if it does not have a renderer then assign a font material to it (via code). Try setting the character size of the textmesh to see if that was the cause (since you didn’t tell me what the value was).
[RequireComponent(typeof(Renderer))]
public class GraphBehaviourClass : MonoBehaviour
{
public Material fontMat;
TextMesh tm;
void Start()
{
tm = gameObject.AddComponent<TextMesh>();
this.renderer.material = fontMat;
tm.text = "TEXT";
tm.fontSize = 2000;
tm.offsetZ = 5;
tm.renderer.material.SetColor("_Color", Color.red);
tm.transform.localPosition = new Vector3(0f, 0f, 0f);
tm.characterSize = 1;
}
}
I tried this
GameObject textObject = new GameObject("text");
textObject.AddComponent("TextMesh");
textObject.AddComponent("MeshRenderer");
TextMesh textMeshComponent = textObject.GetComponent(typeof(TextMesh)) as TextMesh;
textMeshComponent.font = font;
MeshRenderer meshRendererComponent = textObject.GetComponent(typeof(MeshRenderer)) as MeshRenderer;
textMeshComponent.text = "Testing";
meshRendererComponent.materials = fontMaterials;
and does nothing…
MissingComponentException: There is no ‘Renderer’ attached to the “Main Camera” game object, but a script is trying to access it.
You probably need to add a Renderer to the game object “Main Camera”. Or your script needs to check if the component is attached before using it.
On main Camera creates MehsText with default setting, but still no showing…
Well okay, I tried creating it and basically, you’ll need to attach the renderer manually. You must assign a material and font as well as set a initial character size, not font size.
using UnityEngine;
using System.Collections;
public class testtext : MonoBehaviour
{
public Font font;
private TextMesh tm;
// Use this for initialization
void Start ()
{
tm = this.gameObject.AddComponent<TextMesh>();
tm.font = font;
tm.renderer.material = font.material;
tm.characterSize = 1;
tm.text = "test";
}
}
^Code above is tested and working on empty gameobject with renderer component and script only.
Could I draw t in other method instead od Start()?
How I attached the rendere manually? I cant see anything like this on main camera… and font material just Arial, but its fine.
As long as you call it yeah. Just make sure:
- You don’t attach the TextMesh on the camera. It probably will never show up in your game view because it might appear behind the camera.
- Your empty game object that will have the TextMesh attached to MUST have a renderer. You will need to manually attach a renderer.
- You should assign the character size. The default value might be 0.
- You MUST assign a material made for your font. The renderer it has might not have the suitable material for your text. (My test had a game object with a renderer without a material).
Attaching a Renderer:
- Select your game object.
- Component → Mesh → Mesh Renderer
- Profit
UnassignedReferenceException: The variable font of ‘GraphBehaviourClass’ has not been assigned.
You probably need to assign the font variable of the GraphBehaviourClass script in the inspector.
NullReferenceException: Object reference not set to an instance of an object
But the monobehaviour script, which I am calling, is attached to main camera and inside of it, I am creating graph, which is showing and labels, which not…
and
You’ll need to assign your font through the Inspector if you are using my codes by dragging and dropping it in the inspector.