Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  4. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  5. Nominations have been announced for this years Unity Awards. Celebrate the wonderful projects made by your peers this year and get voting! Vote here!
    Dismiss Notice
  6. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  7. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

TextMeshPro giving NullReferenceException when trying to access WordInfo

Discussion in 'Unity UI & TextMesh Pro' started by pravinyadav, Oct 13, 2018.

  1. pravinyadav

    pravinyadav

    Joined:
    Jan 24, 2010
    Posts:
    13
    Hi, I have created a Canvas and attached UI-> TextMeshPro Text.
    Then attached simple script DebugWordInfo.cs to it.


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using TMPro;

    public class DebugWordInfo : MonoBehaviour
    {

    private TMP_Text m_TextComponent;

    private Transform m_Transform;


    private Camera m_Camera;
    private Canvas m_Canvas;

    void Awake()
    {
    // Get a reference to the text component.
    m_TextComponent = gameObject.GetComponent<TMP_Text>();

    // Get a reference to the camera rendering the text taking into consideration the text component type.
    if (m_TextComponent.GetType() == typeof(TextMeshProUGUI))
    {
    m_Canvas = gameObject.GetComponentInParent<Canvas>();
    if (m_Canvas != null)
    {
    if (m_Canvas.renderMode == RenderMode.ScreenSpaceOverlay)
    m_Camera = null;
    else
    m_Camera = m_Canvas.worldCamera;
    }
    }
    else
    {
    m_Camera = Camera.main;
    }
    }

    // Update is called once per frame
    void Update()
    {

    }

    private void Start()
    {
    ShowWords();
    }

    void ShowWords()
    {

    TMP_TextInfo textInfo1 = m_TextComponent.textInfo;

    Debug.Log(textInfo1.wordInfo.Length);
    for (int i = 0; i < textInfo1.wordInfo.Length; i++)
    {
    TMP_WordInfo wInfo = textInfo1.wordInfo[i];
    Debug.Log(wInfo.GetWord());

    }

    }
    }

    It is giving error
    NullReferenceException: Object reference not set to an instance of an object
    TMPro.TMP_WordInfo.GetWord ()

    Pls help!
     
  2. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    1,863
    Whenever properties of a text object change, this text object will need to be regenerated to reflect these changes on these properties. This regeneration occurs just before the frame is rendered which is after LateUpdate() and just before culling occurs in OnPreCull().

    In your script, you are trying to check the content of the textInfo and more specifically wordInfo but you are doing so before the text object has been generated. As a result, you are getting invalid data and this null reference when trying to access GetWord() because no word exist at this point.

    Most of the time, we don't need to access any of this information until the text object has been generated / updated. However, in those cases where we need access to this information (ie. before the normal update cycle), you can force the text object to be updated right away by using TMP_Text.ForceMeshUpdate(). Calling this function will force the text object to be update right away and thus populating the textInfo and other relevant data structures.
     
  3. pravinyadav

    pravinyadav

    Joined:
    Jan 24, 2010
    Posts:
    13
    Hi Stephan,
    Thanks for quick reply.
    Actually I want to create a Text Fx in which UI Text breaks out from message string and word by word it approaches towards camera and fade out. I have figured 2nd part of it.
    Pls have a look at attached link.
    https://www.dropbox.com/s/tdburto1dr04tgt/2018-10-14 at 18-35-50.mp4?dl=0
    Now my problem is to get world space coordinates of each word in string and spawn the textFx at that location.
    Can U pls give me some tips on how to get world space coordinates (center) of each word at runtime.

    Thanks and Regards,
    Pravin
     
    Last edited: Oct 14, 2018
  4. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    1,863
    You will have to lookup the characterInfo of the characters that make up each word. The characterInfo contains the object space coordinates of each character. You will need to transform those into worldspace.

    Take a look at the TextInfoDebugTool.cs script included with the TMP Examples & Extras. I created that script to allow me to visualize the content of the TextInfo like characters, words, lines, links, etc. Simply add that script to any text object. This script should help you get a better understanding of how to access all that information and to implement what you seek.
     
  5. pravinyadav

    pravinyadav

    Joined:
    Jan 24, 2010
    Posts:
    13
    Stephan, Thanks for tips. I am working on it and getting good results.
     
    Stephan_B likes this.