Question by
Psyco92 · Mar 25, 2020 at 05:31 PM ·
scene-loadingasynchronous
Accessing asynchronously loaded objects
I am in a loading scene, it has asynchronously loaded another scene.
I need to run a method on an object found in the newly loaded scene.
I need that to happen before the loading scene closes.
This has been unanswered for 2 YEARS : Here
public static string sceneToLoad = "Home";
public static void LoadScene(string name)
{
sceneToLoad = name;
SceneManager.LoadScene("Loading");
}
void Start()
{
StartCoroutine(LoadYourAsyncScene());
}
IEnumerator LoadYourAsyncScene()
{
AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(sceneToLoad);
asyncLoad.allowSceneActivation = false;
while (asyncLoad.progress < 0.9f)
{
yield return null;
}
Debug.Log("loaded scene");
Scene newScene = SceneManager.GetSceneByName(sceneToLoad);
Debug.Log("loadsed : " + newScene.isLoaded);
GameObject[] rootObjects = newScene.GetRootGameObjects();
Debug.Log("object count : " + rootObjects.Length);
//do things to found objects
asyncLoad.allowSceneActivation = true;
}
Above is my attempt at scripting this, but the scene is not loaded or has any root objects till after the scene is activated, which is obviously too late and makes a loading scene pointless
Comment
Your answer