Issues with update and coroutines
public void Update()
{
if (ExecuteOnMainThread.Count > 0)
{
ExecuteOnMainThread.Dequeue().Invoke();
}
}
public readonly static Queue<Action> ExecuteOnMainThread = new Queue<Action>();
/// <summary>
/// Load all the configured scenes
/// </summary>
public void LoadAllScenes()
{
ExecuteOnMainThread.Enqueue(() => { StartCoroutine(LoadScenes()); });
}
private IEnumerator LoadScenes()
{
yield return null;
Debug.Log("SceneLoader.LoadScenes() is invoked " );
// rest of the scene loading follows from here
}
I have issues with above code, some times Dequeue happens twice though Enqueue coroutine happens once. Some worker thread which is getting message from other process enqueue and unity main thread dequeue and process scene loading. It happens 4/5 times out of 10 on target machine. From debug it is clear that queue is cleared/empty after first update but again later it is becoming 1 but for sure enqueue doesn't happen twice. Can anybody explain what is wrong here?
Answer by Cuttlas-U · Oct 26, 2017 at 05:57 AM
hi; your code is hard for me cant see what u are doing by a simple look; but do u know that corutines can be run more then once and each one will be run separately?
problem is LoadAllScene() function is getting called once and Update function is executing twice. First execution is happening just after LoadAllScene() function call and second execution is happening after few second.. Above script is working fine in Unity editor but issue is happening on windows target machine.
Your answer
Follow this Question
Related Questions
UnwrapCL.exe has stopped working? 8 Answers
Updating Unity means losing compatibility old projects? 0 Answers
fixed update is not being called 1 Answer
Coroutine endlessly looping? 0 Answers