pengwang
pengwang

Reputation: 19956

if coroutine is run end after loadscene

before load oher scene,i have many task to run ,for example save some date to sql and so on,i run the task at coroutine.As you know when call SceneManager.LoadScene,the ondestory is call,so my questions are:

  1. if load other scene until coroutine run finish,or coroutine is break through my task is not finish.
  2. startcoroutine should before SceneManager.LoadScene,or put at the ondestroy 3.if i use SceneManager.LoadSceneAsync not SceneManager.LoadScene,if coroutine will break

Upvotes: 0

Views: 425

Answers (1)

giacomelli
giacomelli

Reputation: 7417

You should call Object.DontDestroyOnLoad in the Awake method of your MonoBehaviour where your coroutine task runs, like described on Unity documentation:

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    void Awake() {
        DontDestroyOnLoad(transform.gameObject);
    }
}

This code will allow your coroutine ends even if another level was loaded.

Upvotes: 1

Related Questions