Search Unity

Seting a startup scene

Discussion in 'Scripting' started by Majk-, Aug 17, 2017.

  1. Majk-

    Majk-

    Joined:
    Jan 29, 2017
    Posts:
    21
    I have about 10 scenes in my project. Scene switching is done using buildIndexes. How do I change the startup scene without having to rewrite all of my scene switching code ? (since changing the scene hierarchy would shift all scene indexes by one.. I know that I could make some calculations to account for that but is that really necessary ?)

    Thanks
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,336
    Don't switch scenes using build indexes!

    The startup scene is always scene 0. Nothing you can do about that. Loading scenes by name is much easier - you don't have to look in the build index to change level correctly.

    Actually, don't every use build indexes for anything. They're incredibly brittle.
     
    hopetolive, bowserscastle and orb like this.
  3. Flavelius

    Flavelius

    Joined:
    Jul 8, 2012
    Posts:
    945
  4. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    1,243
    //What is the other unique scene identifier? - nvm remembered about Unity using strings to identify.
    Also just notices this is obsolete:
    https://docs.unity3d.com/ScriptReference/Application.LoadLevel.html
    Can I get an alternative real quick?
     
  5. Flavelius

    Flavelius

    Joined:
    Jul 8, 2012
    Posts:
    945
    Use the new SceneManager for loading and unloading in the UnityEngine.SceneManagement namespace
     
    FMark92 likes this.
  6. Majk-

    Majk-

    Joined:
    Jan 29, 2017
    Posts:
    21
    Ok, thanks for the insight.
     
  7. astracat111

    astracat111

    Joined:
    Sep 21, 2016
    Posts:
    725
    Code (CSharp):
    1. SceneManager.GetActiveScene().whateverYouWantToGet;
    2. //- > variables are there for you including name, id, all you need.
    What I did for my game, that a lot of people don't prefer, is I created my own global non-destructible game object with a script that manages scene data. That way you can also make that global object's script hold the game menus, player data and display and manage saving and loading screens.

    You're managing then global and local data, the global data is managed by the persistent main game object, and the local data includes scene scripts.

    It can take a while to set up, but if you can get scene ping pong going so to speak you've gotten pretty far.