Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Issue with loading multiple scenes

Discussion in 'Getting Started' started by ancatut, Jul 3, 2018.

  1. ancatut

    ancatut

    Joined:
    Mar 24, 2018
    Posts:
    5
    Hey guys, I'm pretty new to Unity so I'm having a hard time understanding what I'm doing wrong here.
    Basically I'm trying to start the game in the "Menu" scene and load a scene "MainScene" in the background.
    The problem is that, instead of setting the "MainScene" scene as active, both scenes seem to be active
    (the GameObjects from both are visible). I would appreciate your help!

    Here's my code:

    Code (CSharp):
    1. public class GameManager : MonoBehaviour
    2. {
    3.     public static GameManager instance = null;                
    4.     public OSC osc;
    5.     public Crosshair crosshair;
    6.     public Enemy enemy;
    7.     public GameObject targetLockedBtn;
    8.     private SpriteRenderer targetLockedBtn_spriteRenderer;
    9.     private bool shipLocked;
    10.     private bool endOSCsent;
    11.  
    12.     private bool sceneLoaded = false;
    13.  
    14.     void Awake()
    15.     {
    16.         if (instance == null)
    17.             instance = this;
    18.  
    19.         else if (instance != this)
    20.             Destroy(gameObject);
    21.  
    22.         DontDestroyOnLoad(this.gameObject);
    23.     }
    24.  
    25.  
    26.     IEnumerator LoadYourAsyncScene(string name)
    27.     {  
    28.         AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(name, LoadSceneMode.Additive);
    29.  
    30.         while (!asyncLoad.isDone)
    31.         {
    32.             yield return null;
    33.         }
    34.  
    35.         Scene nextScene = SceneManager.GetSceneByName(name);
    36.         if (nextScene.IsValid())
    37.         {
    38.             SceneManager.SetActiveScene(nextScene);
    39.             crosshair = GameObject.Find("Crosshair").GetComponent<Crosshair>();
    40.             enemy = GameObject.Find("Enemy").GetComponent<Enemy>();
    41.         }
    42.     }
    43.  
    44.     private void Start()
    45.     {
    46.         StartCoroutine(LoadYourAsyncScene("Menu"));
    47.     }
    48.    
    49.    // Not including Update() because nothing in there could affect the scenes loading.
    50. }
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    On Line #28, don't use LoadSceneMode.Additive - you want '.Single' (or the default).
     
  3. ancatut

    ancatut

    Joined:
    Mar 24, 2018
    Posts:
    5
    Thanks for your reply, but I don't think I explained well. The additive loading is on purpose -- I'm trying to have several scenes loaded and only set one as active, but I don't know how exactly to do that.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Ah okay, well as far as I know when scenes are loaded additively, they are active in the sense that the game objects are active.. however, only one is the active (primary) scene. Otherwise, it wouldn't really be loaded?
    Unless I misunderstood what you're asking.
     
  5. thadude

    thadude

    Joined:
    May 14, 2013
    Posts:
    5
    i followed the sword and shovels tutorial as well. I found after researching basically you create a parent game object in each scene, make the rest children of the parent and then enable and disable that parent on load of your scene(when you set it active in your state manager) I am not sure if this is correct or not. But that is what I have done until someone tells me otherwise.