I’m trying to create the GUI button which iterates through each project scene and creates a new GameObject instance in it. However, no gameobject appears in any scene.
I have the following method being called when the button is pressed.
private void GenerateDatabaseForEachScene()
{
EditorBuildSettingsScene[] allScenes = EditorBuildSettings.scenes;
foreach (EditorBuildSettingsScene scene in allScenes)
{
EditorSceneManager.OpenScene(scene.scenePath, OpenSceneMode.Single);
GameObject emptyDatabase = CreateEmptyDatabaseForCurrentScene(scene.scenePath);
}
}
And I use static method CreateEmptyDatabaseForCurrentScene() to create an empty gameobject:
private static GameObject CreateEmptyDatabaseForCurrentScene()
{
Scene currentScene = EditorSceneManager.GetActiveScene();
if (!currentScene.IsValid())
{
Debug.LogErrorFormat("A database can be created only if any scene is opened.");
return null;
}
GameObject databaseObject = new GameObject("Database - " + currentScene.name);
databaseObject.SetActive(true);
databaseObject.hideFlags = HideFlags.None;
Debug.Log("The scene of the gameobject created: " + databaseObject.scene.path);
return databaseObject;
}
As you can see, I’m writing the scene where the new gameobject were placed in console. And the correct scene is shown:
But the gameobject doesn’t appear in the scene! Moreover, the method Object.FindObjectsOfType(typeof(GameObject)) returns the array of all objects present in the current scene, but the new gameobject is absent there.
Please, can anybody explain what I’m doing wrong?