0
\$\begingroup\$

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:

Two Secens I have

Scene1 hierarchy:

Scene1 hierarchy

Scene2 hierarchy:

Scene2 hierarchy

Code:

Code:

Result: Only get the open scene's infomation

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.

Error message

So, I followed the suggestion of the error message.

Final code

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)

possible problem

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.

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

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

        [Test]
        public void Test_loadScene()
        {

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

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

Result:

enter image description here

Debug:

enter image description here

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

enter image description here

\$\endgroup\$
  • \$\begingroup\$ Thank you, I will try this way too. Thanks for helping. \$\endgroup\$ – StevenLin Sep 12 at 11:00
  • \$\begingroup\$ If this helps then accept and up vote the answer. \$\endgroup\$ – Swati Sep 12 at 11:04
  • \$\begingroup\$ 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."" \$\endgroup\$ – StevenLin Sep 12 at 11:46
  • \$\begingroup\$ You are right but you can accept an answer if it helps or resolve your issue. \$\endgroup\$ – Swati Sep 12 at 11:48
  • \$\begingroup\$ I see. That's what the check icon for. I didn't know that. Thanks for the tip. \$\endgroup\$ – StevenLin Sep 12 at 11:53
1
\$\begingroup\$

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);
\$\endgroup\$
  • \$\begingroup\$ 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 \$\endgroup\$ – StevenLin Sep 12 at 9:57
  • \$\begingroup\$ 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. \$\endgroup\$ – Uri Popov Sep 12 at 10:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.