LoadScene crashed Unity when used in coroutine or invoke
I'm trying to wait for a few second after a level complete to load the next scene, here's the two versions of code I've tried:
With Invoke
void Update()
{
if (CheckComplete())
{
Debug.Log("Complete");
level += 2;
DrawLine();
Invoke("LoadLevel", 5);
}
}
void LoadLevel()
{
Debug.Log("Try Load Scene");
SceneManager.LoadScene("Level2", LoadSceneMode.Single);
}
And with Coroutine:
void Update()
{
if (CheckComplete())
{
Debug.Log("Complete");
level += 2;
DrawLine();
StartCoroutine("LoadLevel");
}
}
IEnumerator LoadLevel()
{
Debug.Log("Try Load Scene");
yield return new WaitForSeconds(5f);
SceneManager.LoadScene("Level2", LoadSceneMode.Single);
}
Running either of them will freeze Unity after completeting the level. If I have the LoadScene line in the Update() function without trying to delay it however, it works just fine. What is going on?
Why do you believe the problem comes from the Invoke / StartCoroutine and not from CheckComplete
or DrawLine
?
Because everything works fine if I do not use Coroutine or Invoke to change the scene. Here's the CheckComplete() and DrawLine():
bool CheckComplete()
{
Debug.Log("Check Complete");
bool complete = true;
for (int i = 0; i < spawned.Count; i++)
{
if (spawned[i].GetComponent<SpriteRenderer>().color == Color.black)
{
complete = false;
}
}
return complete;
}
void DrawLine() {
for (int i = 0; i < spawned.Count - 1; i++)
{
LineRenderer lr = spawned[i].GetComponent<LineRenderer>();
lr.enabled = true;
lr.startWidth = 0.05f;
lr.endWidth = 0.05f;
lr.startColor = Color.white;
lr.endColor = Color.white;
lr.SetPosition(0, spawned[i].transform.position);
lr.SetPosition(1, spawned[i + 1].transform.position);
}
}
@riaxi18 Have you managed to solve this problem? Unity app crashes on some devices after I reload the scene
Your answer
Follow this Question
Related Questions
Multiple Entrances to levels 0 Answers
how to store and save scene changes for loading later 0 Answers
Invoke method is not called 2 Answers
Scene Load/Unload Fails to Reload 1 Answer
,delay after dying and going back to main menu (new scene) 0 Answers