# Scripting Examples

Use the following methods to manage the Prefab Groups. Refer to `PrefabAndObjectManager.cs` for full detials.

**Activate a Group**\
Activating a group will turn on Game Objects and instantiate Prefabs. Depending on your settings, it may also automatically deactivate other groups.

```csharp
prefabAndObjectManager.ActivateGroup(groupName); // By name
prefabAndObjectManager.ActivateGroup(groupIndex); // By index
prefabAndObjectManager.ActivateGroup(prefabGroup); // By PrefabGroup
```

**Deactivate a Group**\
Deactivating a group will turn off Game Objects and destroy Prefabs. Depending on your settings, it may also automatically activate a group set as "Default".

```csharp
prefabAndObjectManager.DeactivateGroup(groupName); // By name
prefabAndObjectManager.DeactivateGroup(groupIndex); // By index
prefabAndObjectManager.DeactivateGroup(prefabGroup); // By PrefabGroup

// Deactivate all groups of a specific type
prefabAndObjectManager.DeactivateGroupsOfType(groupType);
```

**Check if a Group is Active**\
As groups may share objects, it is possible that a group is not active, fully active, or partially active, where some, but not all, of the objects are active.

```csharp
// By index
if (prefabAndObjectManager.GroupIsActive(groupIndex) == 0){
   Debug.Log("Group is not active");
}

// By PrefabGroup
if (prefabAndObjectManager.GroupIsActive(prefabGroup) == 1){
   Debug.Log("Group is partially active");
}

// By name
if (prefabAndObjectManager.GroupIsActive(groupName) == 2){
   Debug.Log("Group is fully active");
}

// 0 = Not active
// 1 = Partially active (some of the objects are active, but not all)
// 2 = Active
```

**Getting a `List<string>` of Types**\
If you need to obtain a list of all Types, perhaps for UI, this method will come in handy.

```csharp
// Cached version
foreach(var type in prefabAndObjectManager.GroupTypeNames(){
   Debug.Log($"Type: {type}");
}

// You can also use this method, though it will do more computation each time it is called.
foreach(var type in prefabAndObjectManager.GetGroupTypeNames()){
   Debug.Log($"Type: {type}");
}
```

**Getting a `List<PrefabGroup>` with All Groups of a Type**\
If you need to obtain a list of all Types, perhaps for UI, this method will come in handy.

```csharp
foreach(var type in prefabAndObjectManager.GetGroupsOfType(groupType)){
   Debug.Log($"Type: {type}");
}
```

**Get a Prefab Group**\
This will return a PrefabGroup object, for whatever purposes you see fit.

```csharp
var prefabGroup = prefabAndObjectManager.GetPrefabGroup(groupName);
```

**Activate a Random PrefabGroup**\
There are options for a single type, one from all types, or just one random group from a random type.

```csharp
// Random from a specific type
prefabAndObjectManager.ActivateRandomGroup(groupType);

// One random from all types
prefabAndObjectManager.ActivateRandomAllGroups();

// One random group from one random type
prefabAndObjectManager.ActivateRandomGroupType();
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://infinitypbr.gitbook.io/magic-pig-games/equipment-systems/prefab-and-object-manager/scripting-examples.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
