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.
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
NullReferenceException: Object reference not set to an instance of an object
TMPro.TMP_Text.FillCharacterVertexBuffers (Int32 i, Int32 index_X4)
TMPro.TextMeshProUGUI.GenerateTextMesh ()
TMPro.TextMeshProUGUI.GenerateTextMesh ()
TMPro.TextMeshProUGUI.GenerateTextMesh ()
TMPro.TextMeshProUGUI.GenerateTextMesh ()
TMPro.TextMeshProUGUI.GenerateTextMesh ()
TMPro.TextMeshProUGUI.GenerateTextMesh ()
TMPro.TextMeshProUGUI.GenerateTextMesh ()
TMPro.TextMeshProUGUI.GenerateTextMesh ()
TMPro.TextMeshProUGUI.GenerateTextMesh ()
TMPro.TextMeshProUGUI.GenerateTextMesh ()
TMPro.TextMeshProUGUI.OnPreRenderCanvas ()
TMPro.TextMeshProUGUI.Rebuild (CanvasUpdate update)
0. UnityEngine.UI.CanvasUpdateRegistry.PerformUpdate() C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/CanvasUpdateRegistry.cs:149
1. UnityEngine.UI.ScrollRect.LateUpdate()
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.
using UnityEngine;
using System.Collections;
using TMPro;
public class hackyTmpFix : MonoBehaviour {
[SerializedField] private TextMeshProUGUI tPro;
private bool waitAndSetOnEnable = false;
private void OnEnable () { if (waitAndSetOnEnable) StartCoroutine(waitAndSet()); }
public void setText (string valueToSet) {
tPro.richText = false;
tPro.enableAutoSizing = false;
tPro.text = valueToSet;
if (!gameObject.activeInHierarchy) {
waitAndSetOnEnable = true;
} else StartCoroutine(waitAndSet());
}
private IEnumerator waitAndSet () {
yield return new WaitForEndOfFrame();
tPro.richText = true;
tPro.enableAutoSizing = true;
waitAndSetOnEnable = false;
}
}
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
public override void Rebuild(CanvasUpdate update)
{
if (this == null) return;
if (update == CanvasUpdate.Prelayout)
{
if (m_autoSizeTextContainer)
{
m_rectTransform.sizeDelta = GetPreferredValues(Mathf.Infinity, Mathf.Infinity);
}
}
else if (update == CanvasUpdate.PreRender)
{
m_isCalculatingPreferredValues = false; // I added this line
OnPreRenderCanvas();
m_verticesAlreadyDirty = false;
m_layoutAlreadyDirty = false;
if (!m_isMaterialDirty) return;
UpdateMaterial();
m_isMaterialDirty = false;
}
}
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.
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.
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.
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?)
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.
@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.
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