Search Unity

Connecting Two Scenes together with a GameObject?

Discussion in '2D' started by parsahadidi, Jul 26, 2017.

  1. parsahadidi

    parsahadidi

    Joined:
    Jun 27, 2017
    Posts:
    4
    I have created two scenes so far: one title screen and one level. I would like to know how I can build an object so that when I give the command to load the other scene, it does so. I have currently imported the scenemanager and tried using the LoadScene command, but it does nothing for both scenes. I have heard you can create a GameObject to bridge between scenes, but I am not sure where to put it and how to implement it?
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    You could create a singleton-style "LevelManager" component that handles scene changes. This object would start in your first scene, and then it would persist throughout the game by using the DontDestroyOnLoad method.

    For example:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.SceneManagement;
    3.  
    4. public class LevelManager : MonoBehaviour
    5. {
    6.     private static LevelManager _instance;
    7.     public static LevelManager Instance
    8.     {
    9.         get
    10.         {
    11.             if(_instance == null)
    12.             {
    13.                 _instance = FindObjectOfType<LevelManager>();
    14.             }
    15.             return _instance;
    16.         }
    17.  
    18.         private set
    19.         {
    20.             _instance = value;
    21.         }
    22.     }
    23.  
    24.     public UnityEvent OnSceneLoaded;
    25.  
    26.     public Scene CurrentScene { get; private set; }
    27.  
    28.     private void Awake()
    29.     {
    30.         // enforce "there can only be one" rule of Singleton pattern
    31.         if(Instance == null)
    32.         {
    33.             Instance = this;
    34.             DontDestroyOnLoad(gameObject);
    35.         }
    36.         else if(Instance != this)
    37.         {
    38.             Destroy(gameObject); // if one already exists, destroy this one
    39.         }
    40.     }
    41.  
    42.     private void OnEnable()
    43.     {
    44.         SceneManager.sceneLoaded += this.onSceneLoaded;
    45.     }
    46.  
    47.     private void OnDisable()
    48.     {
    49.         SceneManager.sceneLoaded -= this.onSceneLoaded;
    50.     }
    51.  
    52.     private void onSceneLoaded(Scene scene, LoadSceneMode mode)
    53.     {
    54.         CurrentScene = scene;
    55.         OnSceneLoaded.Invoke();
    56.     }
    57.  
    58.     public void LoadScene(string levelName)
    59.     {
    60.         SceneManager.LoadScene(levelName);
    61.     }
    62.  
    63.     public void LoadScene(string levelName, LoadSceneMode mode)
    64.     {
    65.         SceneManager.LoadScene(levelName, mode);
    66.     }
    67.  
    68.     public void LoadScene(int levelIndex)
    69.     {
    70.         SceneManager.LoadScene(levelIndex);
    71.     }
    72.  
    73.     public void LoadScene(int levelIndex, LoadSceneMode mode)
    74.     {
    75.         SceneManager.LoadScene(levelIndex, mode);
    76.     }
    77. }
    With that in your game, from any script you can call LevelManager.Instance.LoadScene("MainMenu") and the game will change to that scene, and this LevelManager will still be there on the other side.

    If you have any questions about that code, feel free to ask.
     
  3. parsahadidi

    parsahadidi

    Joined:
    Jun 27, 2017
    Posts:
    4
    Thanks, but I just figured out my problem was I never built the project. The help is still nice tho.
     
    LiterallyJeff likes this.