# ApplyState

### Introduction

ApplyState can be used to apply a state (StateSave) to a GraphicalUiElement. States can be set by direct StateSave reference, or by unqualified name. Direct StateSave assignment supports states defined in Gum or dynamically created states.

### Code Example - Setting States by Reference

```csharp
// Initialize
var stateToSet = setMeInCode.ElementSave.Categories
    .FirstOrDefault(item => item.Name == "RightSideCategory")
    .States.Find(item => item.Name == "Blue");
setMeInCode.ApplyState(stateToSet);
```

### Code Example - Setting States by Name

```csharp
// Initialize
setMeInCode.ApplyState("Green");
```

### Code Example - Creating Dynamic States

```csharp
// Initialize
var dynamicState = new StateSave();
dynamicState.Variables.Add(new VariableSave()
{
    Value = 300f,
    Name = "Width",
    Type = "float",
    // values can exist on a state but be "disabled"
    SetsValue = true
});
dynamicState.Variables.Add(new VariableSave()
{
    Value = 250f,
    Name = "Height",
    Type = "float",
    SetsValue = true
});
setMeInCode.ApplyState(dynamicState);
```


---

# 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://docs.flatredball.com/gum/code/gum-code-reference/graphicaluielement/applystate.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.
