0
\$\begingroup\$

Complete Unity novice here. First project ever, trying to get familiar and understand the basic aspects of game development with Unity. Trying to work with scenes now.

I have two scenes in my Hierarchy view. I added both to the Build Settings.

The MainScene contains the player sprite and the main game elements, while the TitleScene contains the game name text and a progress bar.

I set TitleScene as the active scene using the option Set Active Scene in the ellipsis context menu of the scene element in the Hierarchy view.

I want to show only one scene on game start, the TitleScene.

The TitleSceneLogic.cs script is attached to a game object in the TitleScene

public class TitleSceneLogic : MonoBehaviour
{    
    void Start()
    {
        print("Title Scene started");
    }
}

The MainSceneLogic.cs script is attached to a game object in the MainScene

public class MainSceneLogic : MonoBehaviour
{    
    void Start()
    {
        print("Main Scene started");
    }
}

Every time I hit Play to see the game running:

  • Both scenes are being shown (overlapping)
  • Console panel shows both Main Scene started and Title Scene started.

Is this really the default behavior in Unity, to load all scenes when the game starts?

I need to show one scene only on game start, the TitleScene.

I gave it a shot by adding this to the MainSceneLogic.cs's Start function

SceneManager.LoadScene("TitleScene");

and indeed only the title scene is shown now, but:

  • I have the feeling this is not the right way, do I really have to manually load a specific scene in the code when the game starts?
  • The logs:
    • Title Scene started
    • Main Scene Started
    • Title Scene started

What is the right way to load only one scene on game start?

\$\endgroup\$
4
  • \$\begingroup\$ The behaviour of hitting Play in the editor (Play Mode) is different from a built executable, which you can test with File -> Build and Run. By default, the executable loads only the first scene in your list of scenes included in the build. What you're observing is a convenience for in-editor testing, so you can open several scenes in the hierarchy and test play mode in all of them simultaneously. This is useful if, in the normal flow of the game to reach Scene X, Scene Y would have been loaded previously - you can set up your hierarchy to launch into that state without a custom script. \$\endgroup\$
    – DMGregory
    May 28, 2023 at 19:21
  • \$\begingroup\$ Mmm this is not very useful if I want to actually test how the game will look like, and see if the scene transitions work. @DMGregory Is this configurable? The only way I found how to emulate a single scene loading at game start is the Unload Scene option on the ellipsis context menu of each scene, which also makes me unable to interact with its elements in the editor. \$\endgroup\$ May 28, 2023 at 19:37
  • \$\begingroup\$ The way to say "play only this scene" is to have only that scene open. By opening multiple scenes at once, you're telling Unity "I want you to play all of these at once when I click the play button". If you really need those open to edit but not to play, you could write a custom script to unload one scene as you enter play mode. This is an exceptional use case though — for most purposes, we want to be able to test a specific set of scenes on demand, rather than always going through the build's start-up flow, so we can quickly test content that might be minutes or hours of gameplay in. \$\endgroup\$
    – DMGregory
    May 28, 2023 at 19:52
  • \$\begingroup\$ How about an option in each scene's context menu to Play? Well, I guess this is just wishful thinking... or a feature request :). Thanks a lot for the help @DMGregory \$\endgroup\$ May 28, 2023 at 20:35

1 Answer 1

1
\$\begingroup\$

I have two scenes in my Hierarchy view. I added both to the Build Settings.

The easier way is to have only one scene in the hierarchy.

From the documentation:

If you need to create large streaming worlds or want to effectively manage multiple scenes at runtime, you can open and edit multiple scenes in the Unity Editor simultaneously. Having multiple scenes open at once also lets you improve the workflow, especially if you often have to edit scenes collaboratively.

But for a normal small start game, one scene at a time is the default work flow.

As you found out, you can switch scenes with SceneManager.LoadScene("Your scene name");. In the build settings you set which scene should load first.

\$\endgroup\$
2
  • \$\begingroup\$ I thought having multiple scenes would be something rather common. Something like a splash screen/start menu for a scene, and another scene for the actual gameplay. Is there a suggested good practice for this common need, without two scenes? Should I just toggle UI elements in a single scene? \$\endgroup\$ May 28, 2023 at 20:37
  • 2
    \$\begingroup\$ It is common to have multiple scenes for menus/gameplay, it's just not common to edit them simultaneously if you don't want to PLAY them simultaneously. Usually I'll open the title scene to work on the title menu, then open the gameplay scene to work on gameplay. I can't think of a single task I've ever done that's demanded having both open at once - so as a "complete Unity novice" on your "first project ever", you might be misjudging what is a "common need" in this engine. You could indeed toggle UI elements if that's something you want to do, rather than switching between scenes. \$\endgroup\$
    – DMGregory
    May 28, 2023 at 20:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .