# In Unity, Is it possible to get gameobjects from non-open Scene?

Sorry for my english mistakes,I treid my best to make myself easy to be understood.

On Unity 2017.1 + C#.

I am trying to make UnitTest to check the potential errors in the scenes. and when I want to get the gameObjects from the two deifferent scenes, I can only get the scene which is open currently in the editor.

How to solve this problem? Or This is impossible to be done in Unity mechanism?

Two Secens I have:

Scene1 hierarchy:

Scene2 hierarchy:

Code:

Result: Only get the open scene's infomation

## My Final Solution

Thanks for the help, you guys help me make progress.

This is the feedback of my work. When I tried to solve my problems with the instruction, I ran into some obstacle.

So, I followed the suggestion of the error message.

And the result is pleasant.

The possible reason I can not get the same result at the begining, It may be because the different test mode(playMode/EditMode)

I will let myself take a break now and not dig deeper at this moment.

Thanks for your help and wish my dicovery can help someone too.

• Have you considered using prefabs to separate your game object configuration from the scenes they're in? – DMGregory Sep 12 at 9:54

You are trying to get scene information before loading the scene try this it worked for me.

        [Test]
{

Scene scen_1 = SceneManager.GetSceneByBuildIndex(0);
Scene scen_2 = SceneManager.GetSceneByBuildIndex(1);

Debug.Log("Scene 1 name: " + scen_1.name);
Debug.Log("Scene 2 name: " + scen_2.name);
}


Result:

Debug:

Make sure both scene are added to "Scene in build"

• Thank you, I will try this way too. Thanks for helping. – StevenLin Sep 12 at 11:00
• If this helps then accept and up vote the answer. – Swati Sep 12 at 11:04
• Because I am a new user, it seem my upvote will not show on the result. I am sorry for that. The message I got when I make upvote >""Thanks for the feedback! Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score."" – StevenLin Sep 12 at 11:46
• You are right but you can accept an answer if it helps or resolve your issue. – Swati Sep 12 at 11:48
• I see. That's what the check icon for. I didn't know that. Thanks for the tip. – StevenLin Sep 12 at 11:53

Hey there I see you are a new contributor. First off let me say that you can paste code directly in your question and that is desired. Secondly Have you tried loading both scenes before performing the test with :

SceneManager.LoadScene("scene_name", LoadSceneMode.Additive);

• Thank for your quick response, I will study the part you mentioned. I will post what I find if I make any progress with this problems. Thank you very much – StevenLin Sep 12 at 9:57
• all you have to do is call SceneManager.LoadScene("scene_name", LoadSceneMode.Additive); where "scene_name" is the actual name of the scene you want to load for every scene before you get the names or do anything else. – Uri Popov Sep 12 at 10:05