- Home /
Find GameObject in another loaded scene
How do I find or reference a GameObject loaded in another scene?
I've tried with GameObject.Find()
and Scene.GetSceneByName("myScene").GetRootGameObjects()
, but they both failed, one because it seemingly doesn't recognize objects outside its current scene and the other because it only returns an empty array.
Edit: the scene was loaded additively
Answer by rh_galaxy · Apr 03, 2021 at 03:22 PM
When you switch scene all objects in that first scene will be deleted (unless they are set as DoNotDestroy). Also you can load a scene in Additive mode, that will keep objects until you unload that scene.
I forgot to add that the scene was loaded additively to the first one, thanks for re$$anonymous$$ding me
Answer by Llama_w_2Ls · Apr 03, 2021 at 05:50 PM
Instead of using Scene.GetSceneByName, why not use one of the overloads for SceneManager.LoadScene, which returns a Scene. For example:
var parameters = new LoadSceneParameters(LoadSceneMode.Additive);
var scene = SceneManager.LoadScene("MyAdditiveScene", parameters);
foreach (var obj in scene.GetRootGameObjects())
{
Debug.Log(obj.name);
}
I don't know if this will change anything but it seems like it would work.
If the scene is additive, GameObject.Find
should still work, I believe. I've never tried it.