Hi.
I have a game manager object in the initial scene and it loads another scene from an asset bundle.
The game manager has Do not destroy on load script and will never be destroyed. So after it loads a new scene, I see “Don’t destroy on Load” and the new scene in the hierarchy.
Then, I would like to get all objects in a new scene, but I have never managed it yet.
I wrote following script, but it just returned the game objects in the “Don’t destroy on Load” scene.
Someone please help me. I need an advice how I can make it happen.
Thank you!
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class SceneImporter : MonoBehaviour
{
public string assetBundleName;
public string sceneName;
public Scene scene;
public void Start()
{
Load();
}
public void Load()
{
StartCoroutine("LoadScene");
GetAllObjectsInLoadedScene();
}
public IEnumerator LoadScene()
{
var resultAssetBundle = AssetBundle.LoadFromFileAsync(Application.streamingAssetsPath + "/scenes/" + assetBundleName);
yield return new WaitWhile(() => resultAssetBundle.isDone == false);
var assetbundle = resultAssetBundle.assetBundle;
// The Application loads the Scene in the background at the same time as the current Scene.
//This is particularly good for creating loading screens. You could also load the Scene by build //number.
AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(sceneName);
//Wait until the last operation fully loads to return anything
while (!asyncLoad.isDone)
{
yield return null;
}
scene = SceneManager.GetSceneByName(sceneName);
SceneManager.SetActiveScene(scene);
}
public void GetAllObjectsInLoadedScene()
{
// Get type of GameObject
foreach (GameObject obj in UnityEngine.Object.FindObjectsOfType(typeof(GameObject)))
{
{
Debug.Log(obj.name);
}
}
}
}