unity scene manager
Here’s a clean, production‑ready Scene Manager system for Unity that handles three scenes:
- Home
- Game
- End
It includes:
- A SceneLoader script
- Optional UI button hooks
- A recommended folder structure
Everything is ready to drop into your project.
๐ฎ 1. Folder Structure (recommended)
Assets/
├── Scripts/
│ └── Systems/
│ └── SceneLoader.cs
├── Scenes/
│ ├── Home.unity
│ ├── Game.unity
│ └── End.unity
๐ง 2. SceneLoader.cs (your main scene manager)
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneLoader : MonoBehaviour
{
public void LoadHome()
{
SceneManager.LoadScene("Home");
}
public void LoadGame()
{
SceneManager.LoadScene("Game");
}
public void LoadEnd()
{
SceneManager.LoadScene("End");
}
public void QuitGame()
{
Application.Quit();
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#endif
}
}
✔ What this script does
- Loads scenes by name
- Works with UI buttons
- Includes a safe Quit function for both Editor and builds
๐ฑ 3. Hooking it up to UI Buttons
- Create a Canvas → Button
- Add the SceneLoader script to an empty GameObject (e.g.,
SceneManager) - Select the button → OnClick()
- Drag the SceneManager object into the slot
- Choose the function:
SceneLoader.LoadHome()SceneLoader.LoadGame()SceneLoader.LoadEnd()
๐งช 4. Example Flow
Home Scene
- “Play” button →
LoadGame()
Game Scene
- When player dies or wins → call
LoadEnd()
End Scene
- “Retry” →
LoadGame() - “Home” →
LoadHome()
Using a UI Button to load scenes in Unity is simple once you know the flow. You connect a button’s OnClick() event to a function in your script that calls SceneManager.LoadScene(). Here’s the full process laid out cleanly so you can wire it up without confusion.
๐ฎ Step‑by‑Step: Make a Button Load a Scene
1️⃣ Create your scenes
Make sure your scenes exist and are added to Build Settings:
- Go to File → Build Settings
- Click Add Open Scenes for each scene
- Ensure the names match exactly (e.g.,
"Home","Game","End")
Unity can only load scenes that appear in this list.
2️⃣ Create a SceneLoader script
This script contains the functions your button will call.
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneLoader : MonoBehaviour
{
public void LoadSceneByName(string sceneName)
{
SceneManager.LoadScene(sceneName);
}
}
Attach this script to an empty GameObject in your scene (e.g., SceneManager).
3️⃣ Add a UI Button
- Right‑click in the Hierarchy → UI → Button
- Unity creates a Canvas + EventSystem automatically
- Select the Button in the Hierarchy
4️⃣ Connect the Button to the SceneLoader
With the Button selected:
Scroll to the Button (Script) component
Find the OnClick() list
Click the + button to add a new event
Drag your SceneManager GameObject (the one with the script) into the empty slot
In the dropdown, choose:
SceneLoader → LoadSceneByName(string)A text field appears — type the scene name exactly:
"Home""Game""End"
Example:
If this button should start the game, type:
Game
๐งช Example Setup
Home Scene
- Play button → loads
"Game"
Game Scene
- Game Over trigger → loads
"End"
End Scene
- Retry button → loads
"Game" - Home button → loads
"Home"
๐ง Why this method is ideal
- You only need one function for all scenes
- Easy to reuse across menus
- Clean and scalable for bigger projects
If you want, I can also show you:
- How to add fade transitions
- How to load scenes asynchronously with a loading bar
- How to make a persistent GameManager that survives scene loads
Just tell me what direction you want to take your project.
Comments