Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

TextMeshProUGUI Errors when displaying anything but direct string. [solved]

Discussion in 'Scripting' started by t-heo, Dec 10, 2019.

  1. t-heo

    t-heo

    Joined:
    Aug 22, 2019
    Posts:
    24
    Hi!

    I keep having issues with TextMeshProUGUI. I have set it up correctly:

    Code (CSharp):
    1. public class MainCode4 : MonoBehaviour
    2. {
    3.     public TextMeshProUGUI displaytext;
    4.     private string test1;
    5.  
    6. void Start(){
    7.         test1 = "say / type";
    8.         displaytext.text = test1;
    Up until here everything works; the TextMeshProUGUi is "say / type".
    However, I get an error immediately when I run program and try to display it anything else, like a .name.

    For example:

    Code (CSharp):
    1.             if (Physics.Raycast(transform.position, fwd, 100)){
    2.                 Transform objectHit = hit.transform;
    3.                 test1 = objectHit.name;
    4.                 displaytext.text = test1;
    Returns: "TextMesh Pro NullReferenceException: Object reference not set to an instance of an object".
    The text mesh is added correctly in the inspector (of course as the first code works, displaying everything correctly).

    Help is appreciated!
     
    Last edited: Dec 12, 2019
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    Use [code ]code tags to post code[/code], and copy the full text of the error out of the console (including the lines below it which include things like line numbers).

    I suspect this doesn't actually have anything to do with TextMeshProUGUI or the string. It doesn't look like you assign a value to "hit" anywhere (usually it'd be one of the parameters in the Raycast() call). So objectHit will be null, and trying to get a null object's name causes a NRE.
     
  3. t-heo

    t-heo

    Joined:
    Aug 22, 2019
    Posts:
    24
    Issue was that some assets were missing; Import TMP Essential Resources solved it.