Skip to content

find_gameobjects and GetAllSceneObjects fail to find nodes when editing prefab in Prefab Stage #547

@L-Fone

Description

@L-Fone

When a prefab is opened in Unity's Prefab Stage (isolation editing mode), the find_gameobjects tool cannot find any GameObjects within the prefab. All searches return empty results.

Root Cause

In GameObjectLookup.cs:250-264, the GetAllSceneObjects method uses SceneManager.GetActiveScene() to get the scene's root objects:

public static IEnumerable GetAllSceneObjects(bool includeInactive)
{
var scene = SceneManager.GetActiveScene(); // ← Problem here
if (!scene.IsValid())
yield break;

  var rootObjects = scene.GetRootGameObjects();
  // ...

}

When in Prefab Stage mode, SceneManager.GetActiveScene() returns the main scene, not the prefab editing scene. The prefab's contents exist in a separate preview scene accessible via PrefabStageUtility.GetCurrentPrefabStage().scene.

Additionally, SearchByPath (line 148) uses GameObject.Find(path) which also cannot find objects in the Prefab Stage.

Suggested Fix

Modify GetAllSceneObjects to check for Prefab Stage first:

public static IEnumerable GetAllSceneObjects(bool includeInactive)
{
// Check Prefab Stage first
var prefabStage = UnityEditor.SceneManagement.PrefabStageUtility.GetCurrentPrefabStage();
if (prefabStage != null)
{
var prefabRoot = prefabStage.prefabContentsRoot;
if (prefabRoot != null)
{
foreach (var go in GetObjectAndDescendants(prefabRoot, includeInactive))
{
yield return go;
}
}
yield break;
}

  // Normal scene mode
  var scene = SceneManager.GetActiveScene();
  if (!scene.IsValid())
      yield break;

  var rootObjects = scene.GetRootGameObjects();
  foreach (var root in rootObjects)
  {
      foreach (var go in GetObjectAndDescendants(root, includeInactive))
      {
          yield return go;
      }
  }

}

Environment

  • Unity 2022.3.x
  • unity-mcp latest version

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions