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

  1. Create a Canvas → Button
  2. Add the SceneLoader script to an empty GameObject (e.g., SceneManager)
  3. Select the button → OnClick()
  4. Drag the SceneManager object into the slot
  5. 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:

  1. Go to File → Build Settings
  2. Click Add Open Scenes for each scene
  3. 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

  1. Right‑click in the Hierarchy → UI → Button
  2. Unity creates a Canvas + EventSystem automatically
  3. Select the Button in the Hierarchy

4️⃣ Connect the Button to the SceneLoader

With the Button selected:

  1. Scroll to the Button (Script) component

  2. Find the OnClick() list

  3. Click the + button to add a new event

  4. Drag your SceneManager GameObject (the one with the script) into the empty slot

  5. In the dropdown, choose:

    SceneLoader → LoadSceneByName(string)
    
  6. 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