How can I get objects in a scene loaded from an asset bundle?

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);
            }
        }
    }
}

I think what you want to do is put that script in the new scene. Right now, it’s running in the first scene; it doesn’t wait for your new scene to load, and then run , if that’s what you had been thinking.
If, in addition to that, you want to exclude the game objects from the first scene, for that you’d have to keep a list in that scene, and then compare it to the next scene, removing/not counting the ones from the first, as you go through the list**. :slight_smile:

Hope that makes sense. heh.

@methos5k answer should be correct. When Load is called, it starts your coroutine and then when that coroutine yields, your GetAllObjectsInLoadedScene runs before the scene is done loading.

Now, you could fix that. If you turn Load into a coroutine, you could yield the LoadScene coroutine. This would wait for the coroutine to finish before calling code after it. (in this case, your method call)

Or, you could call the GetAllObjectsInLoadedScene at the end of your coroutine and see if that works.

Also, just a friendly note, you should use StartCoroutine(LoadScene()); vs the string version you have. The method version is a little faster and if you ever want to pass parameters to a coroutine, you can do so.

Thank you so much for your reply, guys! That is a huge help to me.
@Brathnann ,
I got it how it is working in theory. I will try what you suggested and explained when I get back to my home.
Also, I did not know there is a way to call coroutine with method. Then I do not need to mess around with public valuables as there is a way to pass it multiple parameters!

And yep, it is working!
I just put GetAllGameObjectsInTheScene() function in the end of the coroutine. Such a easy modification made in a airport solved all problem for now!

lol glad you got it working well now :slight_smile: