# FetchGameObjects

> Utility function to fetch all the game objects in a particular scene.

## Definition

* **Type:** Method
* **Namespace:** [UnityEditor.Search](/engine/6000.6/script-reference/unityeditor/search.md)
* **Assembly:** UnityEditor

## FetchGameObjects(Scene)

Utility function to fetch all the game objects in a particular scene.

```csharp
public static GameObject[] FetchGameObjects(Scene scene)
```

### Parameters

**** (\[Scene]\(/engine/6000.6/script-reference/unityengine/scenemanagement/scene)): Scene to get objects from.

### Returns

| Type                                                                        | Description                             |
| --------------------------------------------------------------------------- | --------------------------------------- |
| [GameObject\[\]](/engine/6000.6/script-reference/unityengine/gameobject.md) | The array of game objects in the scene. |

### Remarks

Use `SearchUtils.FetchGameObjects` to create a custom [SearchProvider](/engine/6000.6/script-reference/unityeditor/search/searchprovider.md) that is able to access current scene objects.

### Examples

```csharp
static void OnEnable()
{
    s_GameObjects = SearchUtils.FetchGameObjects().ToArray();
    s_QueryEngine = new QueryEngine<GameObject>();

    // Id supports all operators
    s_QueryEngine.AddFilter("id", go => go.GetEntityId());
    // Name supports only :, = and !=
    s_QueryEngine.AddFilter("n", go => go.name, new[] {":", "=", "!="});

    // Add distance filtering. Does not support :.
    s_QueryEngine.AddFilter("dist", DistanceHandler, DistanceParamHandler, new[] {"=", "!=", "<", ">", "<=", ">="});
}
```

```csharp
static IEnumerator SearchItems(SearchContext context, SearchProvider provider)
{
    var query = s_QueryEngine.ParseQuery(context.searchQuery);
    if (!query.valid)
        yield break;

    var filteredObjects = query.Apply(s_GameObjects);
    foreach (var filteredObject in filteredObjects)
    {
        yield return provider.CreateItem(filteredObject.GetEntityId().ToString(), null, null, null, filteredObject.GetEntityId());
    }
}
```

## FetchGameObjects()

Utility function to fetch all the game objects in a particular scene.

```csharp
public static IEnumerable<GameObject> FetchGameObjects()
```

### Returns

| Type                                                                                                        | Description                             |
| ----------------------------------------------------------------------------------------------------------- | --------------------------------------- |
| [IEnumerable\<GameObject>](https://learn.microsoft.com/dotnet/api/system.collections.generic.ienumerable-1) | The array of game objects in the scene. |

### Remarks

Use `SearchUtils.FetchGameObjects` to create a custom [SearchProvider](/engine/6000.6/script-reference/unityeditor/search/searchprovider.md) that is able to access current scene objects.

### Examples

```csharp
static void OnEnable()
{
    s_GameObjects = SearchUtils.FetchGameObjects().ToArray();
    s_QueryEngine = new QueryEngine<GameObject>();

    // Id supports all operators
    s_QueryEngine.AddFilter("id", go => go.GetEntityId());
    // Name supports only :, = and !=
    s_QueryEngine.AddFilter("n", go => go.name, new[] {":", "=", "!="});

    // Add distance filtering. Does not support :.
    s_QueryEngine.AddFilter("dist", DistanceHandler, DistanceParamHandler, new[] {"=", "!=", "<", ">", "<=", ">="});
}
```

```csharp
static IEnumerator SearchItems(SearchContext context, SearchProvider provider)
{
    var query = s_QueryEngine.ParseQuery(context.searchQuery);
    if (!query.valid)
        yield break;

    var filteredObjects = query.Apply(s_GameObjects);
    foreach (var filteredObject in filteredObjects)
    {
        yield return provider.CreateItem(filteredObject.GetEntityId().ToString(), null, null, null, filteredObject.GetEntityId());
    }
}
```
