SceneManager: how can I find GameObject in another scene?
So I have a sceneManager script in the active scene and so if I use GameObject.Find it will only find gameobjects in the same scene that the script is in correct? What I would like to do is find a GameObject that is in another loaded scene and I can get the root objects with this:
SceneManager.GetSceneByName(sceneToLoad).GetRootGameObjects();
I have made it so there is only 1 root GameObject so how can I accesses it because I want the root objects transform.position value but this returns an array.
My only other option it seems is to move the sceneManager script and ALL other GameObjects it is using over to the other loaded scene, getting the transform.position value then moving it all back to the original scene and that is not a very good way to do it in my opinion.
A SceneManager.FindGameObjectInScene(GameObject go, Scene name); would be very helpful, should request it to be added.
the return value never switches to a single value if it's able to return multiple. in your case the returned array should have one entry. use [0] on it to get your root object
Yes it will return [0] since there is only one GameObject in the array but how can I get the GameObject the 0 in the array represents so I can get it's position from it's transform?
Answer by jgodfrey · Feb 23, 2016 at 01:26 AM
You want something like this:
gameObject[] goArray = SceneManager.GetSceneByName(sceneToLoad).GetRootGameObjects();
if (goArray.Length > 0)
{
gameObject rootGo = goArray[0];
// Do something with rootGo here...
}
I swear I tried this though but it seems to work now, thanks.
Your answer
Follow this Question
Related Questions
how to spawn a player prefab on a server 0 Answers
There is a problem with my script... 0 Answers
[SOLVED] How to load next level? 0 Answers
Reading from file (C#) + Information from scene to scene (C#) 0 Answers
how do i take whats on one UI canvas and make it apear on another in real time(for a card game) 0 Answers