I’m not sure why, but I installed TMPro in my project, and it’s not letting me get a reference to Text Mesh Pro. Here’s my script,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class EndStairsPlacer : MonoBehaviour
{
[SerializeField] Vector3 stairOffset;
[SerializeField] GameObject endStair,obstacle, finalWaypoint;
[SerializeField] int stairCount;
// [SerializeField] TextMeshPro[] texts;
// Start is called before the first frame update
void Start()
{
PlaceStairs();
}
void PlaceStairs()
{
for (int i = 0; i < stairCount; i++)
{
GameObject stair = Instantiate(endStair, transform.position + stairOffset * i * endStair.transform.localScale.magnitude/ 2, Quaternion.identity,transform);
if (i == 0) Instantiate(obstacle,stair.transform.position - -transform.right * 1.6f,Quaternion.identity,transform);
if (i == stairCount - 1) Instantiate(finalWaypoint, stair.transform.position, Quaternion.identity,GameObject.Find("WaypointHolder").transform);
stair.GetComponentInChildren<TextMeshPro>().text = "X" + (i + 1).ToString() ;
}
}
}
As you can see, I am using the proper namespace, but the final line is throwing an error. I can’t get a reference to any of the classes in Text Mesh Pro. even though the namespace isn’t throwing an error. I can only get a reference to “TextMesh” which I heard is Unity’s old 3D text component. I can’t even add a reference to a TextMeshProUGUI object or anything. I’ve tried reinstalling Text Mesh Pro but I still get the same issue.
Well, the main issue is it’s not letting me get a reference to anything in the TMPro namespace. The error is just a null reference exception at
stair.GetComponentInChildren().text = “X” + (i + 1).ToString() ;
The error shows up in Visual Studio, not Unity.
As I explained the issue is not the error, it seems I can’t get references to any of the TMPro components.
This doesn’t make any sense because null reference exceptions are something that occurs when you run your game, so they shouldn’t be showing in Visual Studio, only in Unity. Could you share the exact text of the error?
Sorry, I’m out of it today. It was a “Namespace or type does not exist” error. I just tried it again to show you what the error said and it’s working now. Weird. Well, thanks, I guess haha
TMP reference is a bit confusing, but I think a thing to keep in mind is TMPro is the namespace, so every time you reference TMP, just type TMP instead of textMesh.
I generally use TMP_Text as well as it covers both of these types, but either of the more specific types are acceptable as well, assuming you’r using the right one.