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.

Bug TMP_Text textInfo is initialized on Awake!

Discussion in 'UGUI & TextMesh Pro' started by laurentlavigne, Feb 28, 2021.

  1. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,006
    which isn't the proper way to do that
    why?
    because if the TMP is deactivated and a script accesses textInfo before that tmp was activated it'll nullref
    easy to go around that when we know about it but that's not the proper way to do init for a Unity package
    it should be a lazy pull, init when the property is being accessed like this
    Code (CSharp):
    1.         public TMP_TextInfo textInfo
    2.         {
    3.             get { if (!m_textInfo) m_textInfo = InitTextInfo(); return m_textInfo; }
    4.         }
     
  2. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,006
  3. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,588
    I'll revise the property to return a new instance / empty TMP_TextInfo if accessed prior to awake.
     
  4. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,006
    awesome! thanks
     
    Stephan_B likes this.