This is sort of an extremely specific question. Apologies if it is confusing.
I have two scenes - SceneA and SceneB - running simultaneously in Unity. SceneB is a proxy of SceneA so I will commonly be copying full gameObjects from SceneA to SceneB. The problem is that when any gameObject in SceneB executes Update(), it doesn’t have the correct scene active.
To illustrate the problem, SceneA has a gameObject with a script that contains this code:
private void Update()
{
Scene activeScene = SceneManager.GetActiveScene();
Scene myScene = this.gameObject.scene;
Debug.Log("Active Scene is '" + activeScene.name + "'; This scene is " + myScene.name);
}
This will output “Active Scene is SceneA; This scene is SceneA”
However, when I copy this gameObject to SceneB, the output will be “Active Scene is SceneA; This scene is SceneB”.
My goal is to have the output be “Active Scene is SceneB; This scene is SceneB”.
Obviously, I could simply write into the code to change the active scene with SceneManager.SetActiveScene(SceneB)
, but since my goal is to copy any gameObject with any script from SceneA to SceneB, adding this code to every single script is not a feasible.
So it seems like I must have some kind of functionality that senses when the Update() is about to be called in SceneB and change the active scene to SceneB just before Updates() are executed.
I thought I might be able to do this by adding an event to the player loop, maybe by strategically placing a custom event in the loop at the right place. Unfortunately, I do not think this is a solution either. The problem here is that the events of the player loop is interlaced across scenes. For example:
- SceneA Update()
- SceneB Update()
- SceneA LateUpdate()
- SceneB LateUpdate()
Furthermore, the execution of these events across gameObjects doesn’t even seem to be grouped by scene, but rather by the order the gameObjects were created at runtime.
- SceneA Update() (GameObject1)
- SceneB Update() (GameObject2)
- SceneA Update() (GameObject3)
- SceneA LateUpdate() (GameObject1)
- SceneB LateUpdate() (GameObject2)
- SceneA LateUpdate() (GameObject3)
How can I reliably manage to change the active scene so that Update() always has the scene that the GameObject lives in set to be the active scene without making calls to SceneManager.SetActiveScene(…) in the Update() event inside of every script?