Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

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,421
    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,421
  3. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,596
    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,421
    awesome! thanks
     
    Stephan_B likes this.