0
\$\begingroup\$

Scenes are confusing the hell out of me and I'm pretty sure they should be simple to understand.

When the game loads, only the current scene has a buildIndex != -1. In order give it a 'real' build index I need to load it by path or name (because there's no build index).

The scenes are ten game scenes, one title scene and one persistent scene (which has the player and the status bar with the score etc.).

Should I load a scene every time I change scene and then load additive afterwards or should I be using Set Active Scene?

All the scenes are ticked in the Build Settings.

Build Settings

I'm looking at the index with this:

private void Start()
{
    var sceneName = "Persistent";
    var sceneBeforeLoading = SceneManager.GetSceneByName(sceneName);
    Debug.Log($"Scene {sceneBeforeLoading.name}, BI = {sceneBeforeLoading.buildIndex}");
    SceneManager.LoadScene(sceneName);
    var sceneAfterLoading = SceneManager.GetSceneByName(sceneName);
    Debug.Log($"Scene {sceneAfterLoading.name}, BI = {sceneBeforeLoading.buildIndex}");
}

Which outputs:

----------------------------------------------
Scene , BI = -1
UnityEngine.Debug:Log(Object)
Title:Start() (at Assets/Scripts/Title.cs:10)
----------------------------------------------
Scene Persistent, BI = 1
UnityEngine.Debug:Log(Object)
Title:Start() (at Assets/Scripts/Title.cs:13)
----------------------------------------------
\$\endgroup\$
  • 1
    \$\begingroup\$ If you go to File > Build Settings, do you have any scenes selected (checkbox checked) in the list of “Scenes in Build”? \$\endgroup\$ – Leon Oct 19 at 15:09
  • \$\begingroup\$ SceneManager.LoadScene \$\endgroup\$ – Seyed Morteza Kamali Oct 19 at 15:36
  • \$\begingroup\$ @Leon Aye, all ticked too. \$\endgroup\$ – BanksySan Oct 19 at 15:50
  • \$\begingroup\$ @BanksySan when you say that only the current scene has a buildIndex !=1, where are you seeing all the other -1’s? \$\endgroup\$ – Leon Oct 19 at 16:03
  • \$\begingroup\$ @Leon I added the code I used to show it above, along with the debug log. \$\endgroup\$ – BanksySan Oct 19 at 16:51
1
\$\begingroup\$

I am not sure if you have read the documentation or not on the matter but I would review here SceneManager

The build index is the numbers to the right on your pic of the build settings so you have a choice of 0-12 to load by index.

if loading by name you can load it

 SceneManager.LoadScene(scenename);

or by index

SceneManager.LoadScene(sceneANumber);

you can also load the scene asynchronously using the index

SceneManager.LoadSceneAsync(0);

or by path if you have them bundled in a assetbundle of scenes

 private AssetBundle myLoadedAssetBundle;
private string[] scenePaths;

// Use this for initialization
void Start()
{
    myLoadedAssetBundle = AssetBundle.LoadFromFile("Assets/AssetBundles/scenes");
    scenePaths = myLoadedAssetBundle.GetAllScenePaths();
}

void OnGUI()
{
    if (GUI.Button(new Rect(10, 10, 100, 30), "Change Scene"))
    {
        Debug.Log("Scene2 loading: " + scenePaths[0]);
        SceneManager.LoadScene(scenePaths[0], LoadSceneMode.Single);
    }
}
\$\endgroup\$
  • \$\begingroup\$ Hope that helped to understand the scenes and build index better. Also noting: that every scene in your index != -1. \$\endgroup\$ – Justin Markwell Oct 20 at 16:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.