How to Create a Scene in Unity: Step-by-Step Guide
To create a
Scene in Unity, go to the File menu and select New Scene. You can also create scenes programmatically using SceneManager.CreateScene("SceneName") and then load them with SceneManager.LoadScene.Syntax
Unity scenes can be created in two main ways: through the Unity Editor or by scripting with the SceneManager class.
- Editor: Use
File > New Sceneto create a new scene manually. - Scripting: Use
SceneManager.CreateScene(string sceneName)to create a scene in code. - To load a scene, use
SceneManager.LoadScene(string sceneName).
csharp
using UnityEngine.SceneManagement; // Create a new scene by name Scene newScene = SceneManager.CreateScene("MyNewScene"); // Load a scene by name SceneManager.LoadScene("MyNewScene");
Example
This example shows how to create a new scene called "TestScene" and then load it when a key is pressed.
csharp
using UnityEngine; using UnityEngine.SceneManagement; public class SceneCreator : MonoBehaviour { void Start() { // Create a new scene named "TestScene" SceneManager.CreateScene("TestScene"); Debug.Log("Scene 'TestScene' created."); } void Update() { // Load the scene when space key is pressed if (Input.GetKeyDown(KeyCode.Space)) { SceneManager.LoadScene("TestScene"); Debug.Log("Scene 'TestScene' loaded."); } } }
Output
Scene 'TestScene' created.
(When space key pressed) Scene 'TestScene' loaded.
Common Pitfalls
Common mistakes when creating scenes in Unity include:
- Trying to load a scene that is not added to the Build Settings, which causes errors.
- Creating scenes but forgetting to save them in the Editor, losing changes.
- Using
CreateScenebut not switching to or loading the new scene, so it doesn't appear.
Always add scenes to Build Settings for loading by name and save scenes after editing.
csharp
/* Wrong way: Loading a scene not added to Build Settings */ SceneManager.LoadScene("NonExistentScene"); // This will cause an error /* Right way: Add scene to Build Settings and then load */ // Add scene in Editor: File > Build Settings > Add Open Scenes SceneManager.LoadScene("ExistingScene");
Quick Reference
| Action | Method | Description |
|---|---|---|
| Create Scene | SceneManager.CreateScene(string sceneName) | Creates a new empty scene with the given name. |
| Load Scene | SceneManager.LoadScene(string sceneName) | Loads a scene by its name or index. |
| Save Scene (Editor) | File > Save Scene | Saves the current scene in the Unity Editor. |
| Add Scene to Build | File > Build Settings > Add Open Scenes | Includes the scene in the build for loading by name. |
Key Takeaways
Create scenes in Unity Editor via File > New Scene or by scripting with SceneManager.CreateScene.
Always add scenes to Build Settings before loading them by name to avoid errors.
Save scenes in the Editor to keep your changes.
Use SceneManager.LoadScene to switch between scenes during gameplay.
Check for typos in scene names when loading scenes programmatically.