Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags

ArgumentOutOfRangeException occurs when I set text to SuperTextMesh

A topic by nukenin-waken created Jul 30, 2021 Views: 283 Replies: 3
Viewing posts 1 to 4
(2 edits)

Hello, KaiClavier

I purchased Super Text Mesh and I have trouble.

When I try to set text, sometimes the following error occurs.

ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
System.ThrowHelper.ThrowArgumentOutOfRangeException (System.ExceptionArgument argument, System.ExceptionResource resource) (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.ThrowHelper.ThrowArgumentOutOfRangeException () (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.Collections.Generic.List`1[T].get_Item (System.Int32 index) (at <695d1cc93cca45069c528c15c9fdd749>:0)
UnityEngine.UI.Collections.IndexedSet`1[T].get_Item (System.Int32 index) (at /Applications/2020.3.8f1/Unity.app/Contents/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/SpecializedCollections/IndexedSet.cs:113)
UnityEngine.UI.CanvasUpdateRegistry.PerformUpdate () (at /Applications/2020.3.8f1/Unity.app/Contents/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/CanvasUpdateRegistry.cs:221)
UnityEngine.Canvas.SendWillRenderCanvases () (at /Users/bokken/buildslave/unity/build/Modules/UI/ScriptBindings/UICanvas.bindings.cs:83)
UnityEngine.Canvas.ForceUpdateCanvases () (at /Users/bokken/buildslave/unity/build/Modules/UI/ScriptBindings/UICanvas.bindings.cs:71)
SuperTextMesh.Rebuild (System.Single startTime, System.Boolean readAutomatically, System.Boolean executeEvents) (at Assets/Clavian/SuperTextMesh/Scripts/SuperTextMesh.cs:1480)
SuperTextMesh.Rebuild (System.Single startTime, System.Boolean readAutomatically) (at Assets/Clavian/SuperTextMesh/Scripts/SuperTextMesh.cs:1465)
SuperTextMesh.Rebuild () (at Assets/Clavian/SuperTextMesh/Scripts/SuperTextMesh.cs:1455)
SuperTextMesh.set_text (System.String value) (at Assets/Clavian/SuperTextMesh/Scripts/SuperTextMesh.cs:763)

It happens sometimes, not always.

I don't know why it happens.

In my project, I try to find all SuperTextMeshes in my scenes and set texts on launching.

Is it possible that it makes the problem?

Could you give me some advice?

Unity version: 2020.3.8f1

Text Mesh Pro version: version 1.11.2

Developer (1 edit)

Hmm well the code causing the error seems to happen after STM calls ForceUpdateCanvases, so maybe the canvas registry is being told to update a canvas it doesn't know about yet...?

Is your call to set text on Awake()? Switching it to Start() vould jave different behaviour.

That said, I think I can come up with a better way to make the canvas update, so I want to try that, too. I'll try to reproduce this momentarily!


Edit: I'm trying to reproduce this now, and I can't seem to... (Calling FindObjectsOfType() and setting all text meshes upon Awake()) Can I see a bit of your code to get a better idea of when this is being called? Thank you!

Thank you for your reply.

I call it on Awake, but I use call coroutine, and setting text is called a few frames later from Awake().

As I said, I find all SuperTextMeshes in my scenes and set texts on launching.

I changed it to an async function and it worked.

So the problem is solved now but I'm afraid that the problem will occur again.

Here is my sample code (Some part of the code is omitted, so it doesn't work).

public class MessageDatabase : SingletonScriptableObject<MessageDatabase> {
    public void ApplyAllUIText( Scene scene )
    {
    GameObject[] objs = scene.GetRootGameObjects();
    foreach (var obj in objs) {
    ApplyAllUIText( obj );
    }
    }
    
    public void ApplyAllUIText(GameObject obj)
    {
        Text text = obj.GetComponent<Text>();
    if( text != null )
    {
            InterpretText( ref text );
    }
    
        var tmp_text = obj.GetComponent<TextMeshProUGUI>();
    if( tmp_text != null )
    {
            InterpretText( ref tmp_text );
    }
        
        var stm_text = obj.GetComponent<SuperTextMesh>();
    if( stm_text != null )
    {
            InterpretText( ref stm_text );
    }
    
    var trans = obj.transform;
        for( var i = 0; i < trans.childCount; ++i ) {
    Transform t = trans.GetChild( i );
    ApplyAllUIText( t.gameObject );
        }
    }
    
    public void InterpretText( ref Text t )
    {
        if( t.text.StartsWith( "$$" ) )
        {
            string key = t.text.Substring(2);
            t.text = GetText( key );
        }
    }
    public string GetText(string key)
    {
        ...
    }
}
Developer

I'm trying out your code, and I still can't seem to get it to reproduce... are you trying to use STM with Unity UI, or without? Because... your code is using scene.GetRootObjects() then obj.GetComponent(), so for UI text that will always be the child of a canvas, this code should never grab that text to begin with? Am I understanding your code correctly?