Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Can't Get Reference to TMPro

Discussion in 'Scripting' started by Mashimaro7, Aug 3, 2021.

  1. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    723
    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,
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5.  
    6. public class EndStairsPlacer : MonoBehaviour
    7. {
    8.     [SerializeField] Vector3 stairOffset;
    9.     [SerializeField] GameObject endStair,obstacle, finalWaypoint;
    10.     [SerializeField] int stairCount;
    11.    
    12.    // [SerializeField] TextMeshPro[] texts;
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.         PlaceStairs();  
    17.     }
    18.  
    19.     void PlaceStairs()
    20.     {
    21.         for (int i = 0; i < stairCount; i++)
    22.         {
    23.             GameObject stair = Instantiate(endStair, transform.position + stairOffset * i * endStair.transform.localScale.magnitude/ 2, Quaternion.identity,transform);
    24.             if (i == 0) Instantiate(obstacle,stair.transform.position - -transform.right * 1.6f,Quaternion.identity,transform);
    25.             if (i == stairCount - 1) Instantiate(finalWaypoint, stair.transform.position, Quaternion.identity,GameObject.Find("WaypointHolder").transform);
    26.            stair.GetComponentInChildren<TextMeshPro>().text = "X" + (i + 1).ToString() ;
    27.         }
    28.     }
    29.  
    30. }
    31.  
    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
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    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.
     
  3. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    723
    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<TextMeshPro>().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.
     
    Last edited: Aug 3, 2021
    swittrock likes this.
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    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?
     
  5. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    723
    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
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    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.
     
    CMRE and Mashimaro7 like this.
  7. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    723
    Thanks, I'll try that next time :)
     
  8. mikeohc

    mikeohc

    Joined:
    Jul 1, 2020
    Posts:
    215
    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.
     
    OrangeTangerine1 likes this.
  9. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    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.
     
    Mashimaro7 and mikeohc like this.
  10. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    723
    Good to know, I didn't know there was an all-inclusive reference :)
     
  11. Spelborea

    Spelborea

    Joined:
    May 20, 2020
    Posts:
    11
    Just Regenerate the files of external tools on preferences and will work.