Unit test for finding an object in all scenes

Hello, I am trying to make a test to check if an particular object is in all scenes in my project, in this case the object is Transition.
However the current snippet won’t iterate throw all scenes.
I tried with SceneManager.sceneCount also but nothing. How can I do it?

[Test]
public void ObjectInAllScenes()
{
    var rootObjects = new List<GameObject>();

    for (int j = 0; j < SceneManager.sceneCountInBuildSettings; j++)
    {
        var isIn = false;
        var scene = SceneManager.GetSceneAt(j);
      
        scene.GetRootGameObjects(rootObjects);

        for (int i = 0; i < rootObjects.Count && !isIn; ++i)
        {
            if (rootObjects[i].name == "Transition")
            {
                isIn = true;
            }
        }

        if (!isIn)
        {
            Assert.Fail("Fail: " + scene.name);
        }
    }

    Assert.Pass();
}

Here is something I wrote yesterday to do this.

var assets = AssetDatabase.FindAssets ("t:Scene");
for(int i = 0; i < assets.Length; ++i)
{
    var path = AssetDatabase.GUIDToAssetPath (assets[i]);
    EditorSceneManager.OpenScene (path, OpenSceneMode.Single);

    // Do stuff
}

You should use EditorSceneManager for editor scripts as the other version is designed for runtime and defers some work to the next frame.

1 Like

Thanks it is working!!!

1 Like