Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    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. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  4. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

TextMesh Pro NullReferenceException on TMPro.TMP_Text.FillCharacterVertexBuffers (Int32 i, Int32 index_X4)

Discussion in 'Unity UI & TextMesh Pro' started by QuantumCalzone, Apr 8, 2017.

  1. QuantumCalzone

    QuantumCalzone

    Joined:
    Jan 9, 2010
    Posts:
    240
    Anyone else getting this error when changing TextMeshProUGUI via scripts?

    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. TMPro.TMP_Text.FillCharacterVertexBuffers (Int32 i, Int32 index_X4)
    3. TMPro.TextMeshProUGUI.GenerateTextMesh ()
    4. TMPro.TextMeshProUGUI.GenerateTextMesh ()
    5. TMPro.TextMeshProUGUI.GenerateTextMesh ()
    6. TMPro.TextMeshProUGUI.GenerateTextMesh ()
    7. TMPro.TextMeshProUGUI.GenerateTextMesh ()
    8. TMPro.TextMeshProUGUI.GenerateTextMesh ()
    9. TMPro.TextMeshProUGUI.GenerateTextMesh ()
    10. TMPro.TextMeshProUGUI.GenerateTextMesh ()
    11. TMPro.TextMeshProUGUI.GenerateTextMesh ()
    12. TMPro.TextMeshProUGUI.GenerateTextMesh ()
    13. TMPro.TextMeshProUGUI.GenerateTextMesh ()
    14. TMPro.TextMeshProUGUI.GenerateTextMesh ()
    15. TMPro.TextMeshProUGUI.OnPreRenderCanvas ()
    16. TMPro.TextMeshProUGUI.Rebuild (CanvasUpdate update)
    I'm still trying to get the exact reproduction steps down but it seems to be caused by stemming from a unity UI call

    This was happening to me in the latest Unity 5.5 release but its still happening with 5.6 and the new TextMeshPro release.
     
  2. QuantumCalzone

    QuantumCalzone

    Joined:
    Jan 9, 2010
    Posts:
    240
    ah wait nvm lol, was setting the text to a null string.

    Just had to do a forum post for me to find that error!
     
  3. QuantumCalzone

    QuantumCalzone

    Joined:
    Jan 9, 2010
    Posts:
    240
    Aw man its happening again! Not from a null string this time since I check the string before the TextMeshProUGUI is set. Any ideas !?! :,(
     
  4. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    1,634
    What are you trying to change via scripting?

    Just changing the text should not result in any of these errors. Perhaps you could provide an example of the script you are using that produces these errors or provide a repro of the scene / project producing these errors.
     
  5. QuantumCalzone

    QuantumCalzone

    Joined:
    Jan 9, 2010
    Posts:
    240
    it's hard to isolate with the since I think the stack trace is cut off from the stack trace "UnityEngine.UI.ScrollRect.LateUpdate()" if I'm not mistaken. I can reproduce the error in my big project just fine. Trying to figure out what exactly is going on to put in an empty project for u...

    the full stack is
    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. TMPro.TMP_Text.FillCharacterVertexBuffers (Int32 i, Int32 index_X4)
    3. TMPro.TextMeshProUGUI.GenerateTextMesh ()
    4. TMPro.TextMeshProUGUI.GenerateTextMesh ()
    5. TMPro.TextMeshProUGUI.GenerateTextMesh ()
    6. TMPro.TextMeshProUGUI.GenerateTextMesh ()
    7. TMPro.TextMeshProUGUI.GenerateTextMesh ()
    8. TMPro.TextMeshProUGUI.GenerateTextMesh ()
    9. TMPro.TextMeshProUGUI.GenerateTextMesh ()
    10. TMPro.TextMeshProUGUI.GenerateTextMesh ()
    11. TMPro.TextMeshProUGUI.GenerateTextMesh ()
    12. TMPro.TextMeshProUGUI.GenerateTextMesh ()
    13. TMPro.TextMeshProUGUI.OnPreRenderCanvas ()
    14. TMPro.TextMeshProUGUI.Rebuild (CanvasUpdate update)
    15. 0. UnityEngine.UI.CanvasUpdateRegistry.PerformUpdate()    C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/CanvasUpdateRegistry.cs:149
    16. 1. UnityEngine.UI.ScrollRect.LateUpdate()
     
  6. QuantumCalzone

    QuantumCalzone

    Joined:
    Jan 9, 2010
    Posts:
    240
    So I couldn't isolate what was broken in an empty project. It had something to do with richText and autoSizing though because when I disabled those the reproducible cases ceased and vice-versa when I re-enabled them.

    I did make a hacky fix though!
    This is my gross code that consistently made the error stop.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using TMPro;
    4.  
    5. public class hackyTmpFix : MonoBehaviour {
    6.  
    7.     [SerializedField] private TextMeshProUGUI tPro;
    8.     private bool waitAndSetOnEnable = false;
    9.  
    10.     private void OnEnable () { if (waitAndSetOnEnable) StartCoroutine(waitAndSet()); }
    11.  
    12.     public void setText (string valueToSet) {
    13.  
    14.         tPro.richText = false;
    15.         tPro.enableAutoSizing = false;
    16.  
    17.         tPro.text = valueToSet;
    18.  
    19.         if (!gameObject.activeInHierarchy) {
    20.             waitAndSetOnEnable = true;
    21.         } else StartCoroutine(waitAndSet());
    22.  
    23.     }
    24.  
    25.     private IEnumerator waitAndSet () {
    26.  
    27.         yield return new WaitForEndOfFrame();
    28.  
    29.         tPro.richText = true;
    30.         tPro.enableAutoSizing = true;
    31.  
    32.         waitAndSetOnEnable = false;
    33.     }
    34.  
    35. }
    36.  
     
  7. Costiome

    Costiome

    Joined:
    Dec 7, 2013
    Posts:
    1
    I encountered the same problem, your fix is working, thank you !
     
  8. RRodriguezMT

    RRodriguezMT

    Joined:
    Sep 29, 2016
    Posts:
    1
    Hey, so I debugged this today and it looks like the problem is that sometimes going into OnPreRenderCanvas() the m_isCalculatingPreferredValues is set to true.

    This in turn makes the function SetArraySizes() - we get there through ParseInputText() - to exit early, which in turn skips the memory allocation for the m_textInfo.meshInfo. The nullref happens when accessing any array in meshInfo which in this case the first one to be accessed happens to be vertices.

    My fix for now is setting the m_isCalculatingPreferredValues to false before the call to OnPreRenderCanvas() inside the Rebuild() function. My reasoning being that if we are rebuilding then we are not just checking the values, we want a full check of the memory, etc. From what I can gather, the worst case scenario is that we don't skip the memory checks/allocations and then the program will run a bit slower, but that is better than a nullrefexception.

    TextMeshProUGUI.cs
    Code (CSharp):
    1. public override void Rebuild(CanvasUpdate update)
    2.         {
    3.             if (this == null) return;
    4.  
    5.             if (update == CanvasUpdate.Prelayout)
    6.             {
    7.                 if (m_autoSizeTextContainer)
    8.                 {
    9.                     m_rectTransform.sizeDelta = GetPreferredValues(Mathf.Infinity, Mathf.Infinity);
    10.                 }
    11.             }
    12.             else if (update == CanvasUpdate.PreRender)
    13.             {
    14.                 m_isCalculatingPreferredValues = false; // I added this line
    15.                 OnPreRenderCanvas();
    16.  
    17.                 m_verticesAlreadyDirty = false;
    18.                 m_layoutAlreadyDirty = false;
    19.  
    20.                 if (!m_isMaterialDirty) return;
    21.  
    22.                 UpdateMaterial();
    23.                 m_isMaterialDirty = false;
    24.             }
    25.         }
    @Stephan_B
     
    Last edited: May 10, 2017
  9. Claytonious

    Claytonious

    Joined:
    Feb 16, 2009
    Posts:
    523
  10. Claytonious

    Claytonious

    Joined:
    Feb 16, 2009
    Posts:
    523
    In our case, disabling Auto Size on one of the many TMProUGUIs in the project fixes the problem for now, but that might break again at any moment so we're going with some kind of code change similar to that proposed by @RRodriguezMT, or a hack that makes the null ref stop and marks the Graphic dirty so it will rebuild again, but eager to hear from @Stephan-B, @Stephan_B how bad an idea the forcing everything to rebuild may or may not be.
     
  11. Stephan-B

    Stephan-B

    Unity Technologies

    Joined:
    Feb 23, 2011
    Posts:
    2,269
    I would need a Repro project (submitted via the bug report with case #) that would enable me to reproduce the behavior. This would allow me to figure out why you are getting the error in the first place and determine how to properly address it.

    Opps! posted on the wrong account
     
    Last edited by a moderator: May 12, 2017
  12. QuantumCalzone

    QuantumCalzone

    Joined:
    Jan 9, 2010
    Posts:
    240
    Wish I could implement this but I'm on the free DLL version of TMP so i guess ill have to wait for an update ^_^
     
  13. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    1,634
    I could still use a bug report that reliably reproduces the reported behavior. If you submit the bug report, please provide me with the case #.
     
  14. slumtrimpet

    slumtrimpet

    Joined:
    Mar 18, 2014
    Posts:
    245
    @Stephan_B

    Hey man, was any progress ever made on this one? We've got an application that's throwing this now in very very rare cases. Working to isolate it, but it's buried deep (and is working 99.9% of the time).

    IndexOutOfRangeException: Array index is out of range.

    Code (csharp):
    1.  
    2. TMPro.TMP_Text.FillCharacterVertexBuffers (Int32 i, Int32 index_X4)
    3. TMPro.TextMeshProUGUI.GenerateTextMesh ()
    4. TMPro.TextMeshProUGUI.GenerateTextMesh ()
    5. TMPro.TextMeshProUGUI.GenerateTextMesh ()
    6. TMPro.TextMeshProUGUI.GenerateTextMesh ()
    7. TMPro.TextMeshProUGUI.GenerateTextMesh ()
    8. TMPro.TextMeshProUGUI.GenerateTextMesh ()
    9. TMPro.TextMeshProUGUI.GenerateTextMesh ()
    10. TMPro.TextMeshProUGUI.GenerateTextMesh ()
    11. TMPro.TextMeshProUGUI.GenerateTextMesh ()
    12. TMPro.TextMeshProUGUI.OnPreRenderCanvas ()
    13. TMPro.TextMeshProUGUI.Rebuild (CanvasUpdate update)
    14. UnityEngine.UI.CanvasUpdateRegistry.PerformUpdate ()
    15. UnityEngine.UI.ScrollRect:set_horizontalNormalizedPosition(Single)
    16.  
     
  15. slumtrimpet

    slumtrimpet

    Joined:
    Mar 18, 2014
    Posts:
    245
    Referring the the TMPro forums and the release notes for 10a:

    Fixed a potential OutOfRangeException error in the FillCharacterVertexBuffers() which could occur when using text auto sizing and layout components. Beta 10a

    Is that the issue I'm hitting?!?

    PS... can you link me over to your forum or someplace where this item is discussed in more detail? I'd like to read up on the background behind this fix to see for myself it's a direct match to what I'm hitting (also, any idea what TMPro rev introduced this issue?)
     
    Last edited: Jun 26, 2017
  16. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    1,634
    Here is the link to the potential thread http://digitalnativestudios.com/forum/index.php?topic=1512.msg11580#msg11580 which can be uncovered by searching for 10a or the error you reported.

    This potential issue would have been around for a while and was more like an oversight on my part.
     
  17. samlabs_lea

    samlabs_lea

    Joined:
    May 1, 2018
    Posts:
    2
    I am seeing the exact same problem in Unity 2018.2.0f2 with the current version of TextMesh Pro that is distributed via the Unity Package Manager. The dirty hack of turning auto size off and on again works.
     
  18. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    1,634
    Can you provide the text and settings that are causing this behavior or some project sample that would enable me to look at it?
     
  19. samlabs_lea

    samlabs_lea

    Joined:
    May 1, 2018
    Posts:
    2
    @Stephan_B I created a minimal repro project although this is not throwing the exception, it still isn't behaving correctly. The text object is inside a prefab which is instantiated by a script. The text does not render until the "AutoSize" property is turned off and then on again using the Inspector. Case 1067717

    I will see if I can put a basic repro case together that throws the exception. Our project reliably throws the exception every time upon instantiating our prefab.
     
  20. RFredW_Exient

    RFredW_Exient

    Joined:
    Mar 29, 2017
    Posts:
    2
    Here’s a fairly small repro case. The (rich) text itself and the presence of a (horizontal in this case) layout group appears to be the key.
    (First time uploading a file here, hope it works)
    I’m running 2018.1.3f1, for what it’s worth.
    * Open up project
    * Open TestScene
    * Run
    * Instantiated blue box doesn’t render text, exception occurs
    * Select text (CanvasUI / PopupContainter / Buttons / Button1 / PopupButtonSticker(Clone)/StickerText)
    * Turn “auto size” off (Text is shown)
    * Turn “auto size” on (Text remains)
     

    Attached Files:

    Claytonious likes this.
  21. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    1,634
    Thank you for providing the Repro, I'll try taking a look in the next few days and provide feedback thereafter.
     
    Claytonious likes this.