- Home /
 
Problems adding scene to build settings via code
I'm working on an editor tool, where I am creating a new scene from code using EditorSceneManager.NewScene, adding an object to it and then saving it.
After that I want to add it to build settings, which is where something goes wrong. No error pops up in the editor, but build settings looks like this after adding: 
My code for creating the scene is as follows:
 // Create new scene, add scene manager prefab, save and then close again.
 var initScene = 
     EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Additive);
 initScene.name = "scene_name_here";
 var activeScene = EditorSceneManager.GetActiveScene();
 EditorSceneManager.SetActiveScene(initScene);
 var smToolObj = new GameObject("scene_name_here");
 var smTool = smToolObj.AddComponent<SceneManagementTool>();
 smTool.SetInput(true);
 EditorSceneManager.SaveScene(initScene, _initScenePath);
 EditorSceneManager.SetActiveScene(activeScene);
 EditorSceneManager.CloseScene(initScene, true);
 
               My code for adding the scene to build settings looks as follows:
 if (EditorBuildSettings.scenes.Length == 0)
         {
             var newBuildSettingScenes = new List<EditorBuildSettingsScene>
             {
                 new EditorBuildSettingsScene(scenePath, true),
             };
             EditorBuildSettings.scenes = newBuildSettingScenes.ToArray();
         }
         else if (EditorBuildSettings.scenes[0].path == scenePath)
         {
             EditorBuildSettings.scenes[0].enabled = true;
         }
         else
         {
             // If it exists already, but is not first, remove it and add it again in front.
             // If it doesn't exist, add it in front.
             var newBuildSettingScenes = new List<EditorBuildSettingsScene>(EditorBuildSettings.scenes.Where(s => s.path != scenePath));
             newBuildSettingScenes.Prepend(new EditorBuildSettingsScene(scenePath, true));
             EditorBuildSettings.scenes = newBuildSettingScenes.ToArray();
         }
 
               The code above should also add the scene as the first one in build settings and make sure that it is enabled.
I also find it weird that my new scene has the guid: 00000000000000000000000000000000. That should be something like: a58e401d4a7717443a2815ac2bc83212, right? 
Can anyone tell me, why this doesn't work? I'm working in Unity 2019.1.4f1
Thanks in advance!
Answer by phillipphoenix · Oct 01, 2019 at 11:02 AM
I found out what the problem was.
When adding scenes to build settings the path must start with "Assets", for instance "Assets/scenes/name_of_scene.unity" instead of "scenes/name_of_scene.unity"
Your answer