- Home /
Graphic Rebuild Loop Error
Trying to add BlueBg (UnityEngine.UI.Image) for graphic rebuild while we are already inside a graphic rebuild loop. This is not supported. UnityEngine.UI.CanvasUpdateRegistry:InternalRegisterCanvasElementForGraphicRebuild(ICanvasElement) UnityEngine.UI.Graphic:SetVerticesDirty() UnityEngine.UI.MaskableGraphic:OnEnable() UnityEngine.UI.Image:OnEnable() UnityEngine.Object:Instantiate(Object, Transform, Boolean)
I have been stuck with this error from past 2 days, i have tried all methods related to canvasregistry, but im still getting this error and on evey single canvas related objects(for ex. Text, images, buttons) inside my Scene and because of that my builds are crashing a lot. Is there anyone who can support me on this query.
Answer by AldeRoberge · May 27 at 11:18 PM
The problem is that you are maybe trying to call a function outside the main thread. You can use something like this :
using System;
using System.Collections.Concurrent;
using Scripts.Utils.Objects;
using UnityEngine;
namespace Scripts.Utils.UnityAsync
{
/// <summary>
/// Dispatch events to be executed on the Unity main thread.
/// </summary>
public class Dispatcher : Singleton<Dispatcher>
{
private static readonly ConcurrentBag<Action> pending = new ConcurrentBag<Action>();
public void Invoke(Action fn)
{
pending.Add(fn);
}
private void Update()
{
InvokePending();
}
private void InvokePending()
{
/*
* TODO fix :
*
* InvalidOperationException: Collection was modified; enumeration operation may not execute.
* Happens when you call Dispatcher.Invoke inside of another Dispatcher.Invoke
*/
while (!pending.IsEmpty)
{
pending.TryTake(out var action);
try
{
action();
}
catch (Exception e)
{
// If there is no Catch, the pending list never clears.
Debug.LogError("Error happened during invoking action with Dispatcher. Error : ");
Debug.LogError(e);
}
}
}
}
}
To invoke code on the main thread :
Dispatcher.Instance.Invoke(() =>
{
// Code to run on the main thread here
});