Can't Get Reference to TMPro

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.

Can anyone help me fix this please? Thanks

A good first step would be to share the error message you are seeing, and explain fi it’s a compile-time error or a runtime error.

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.

1 Like

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

Sometimes VS loses its link to the Unity project for whatever reason.

I’ve found that going to Preferences → External Editors → Generate Project Files (in Unity) usually fixes it.

2 Likes

Thanks, I’ll try that next time :slight_smile:

isn’t textmeshpro component
stair.GetComponentInChildren<TMP_Text>().text?

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.

1 Like

TMP_Text is the parent class of both:

  • TextMeshPro (the 3d, game world version of TextMeshPro)

  • TextMeshProUGUI (the UI version)

https://docs.unity3d.com/Packages/com.unity.textmeshpro@1.0/api/TMPro.TMP_Text.html

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.

2 Likes

Good to know, I didn’t know there was an all-inclusive reference :slight_smile:

Just Regenerate the files of external tools on preferences and will work.