How to wait until additive scene is set to active before calling Start() on GameObjects.

I want to do some logic in a script’s Start() depending on which scene it’s GameObject is in.
However, Start() is called before the scene is set to active, so I cannot use if(!SceneManager.GetActiveScene().name.Equals(Constants.SCENE_MAIN)).

I load my scenes like this:

private IEnumerator LoadAsyncSceneAdditive(string scene)
    {
        AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(scene, LoadSceneMode.Additive);

        Debug.Log("Loading Scene: " + scene + "...");

        while (!asyncLoad.isDone)
        {
            yield return null;
        }

        Debug.Log("Finished loading Scene: " + scene);

        SceneManager.SetActiveScene(SceneManager.GetSceneByName(scene));
    }

How can I switch from Scene A to Scene B, and make sure that no Start() functions are called in Scene B before it is set as the active scene?

Really any solution that will allow a reliable check of which scene the GameObject is in on Start()

I’m not exactly sure, but have you tried playing around with the allow scene activation flag? It looks like it could work for you?

I have a little bit, it seems it’s for delaying the activation of a scene. Which is not what I want. Or did you have some other idea?

That’s a pity. If there really isn’t a way of delaying the calls, I wonder if you would be left with having to keep track of your own ‘current scene’ and refer to that rather than through the Unity API?

I think I might have figured out a solution that will work for me. I put all GameObjects in a Parent-Object with a Script referencing the scene name (manually input via inspector). Then I use transform.root and get the referenced Scene :slight_smile:

Glad you figured out a way round it. :slight_smile:

If you want the scene the GameObject is a member of:

1 Like

Another way is to change all the interesting Start’s to hand-called public inits(). After your waiting for the load is done, you can hand-set whatever you need to, in the new scene, then call init() on them all.

It’s a small pain, but it also makes it so you don’t have to play with script execution order, since you call the init()s in whatever order you want. It’s a “hammer” solution in that it’s not pretty and won’t impress anyone with clever use of Unity features, but it always works.

1 Like