# SetProperty

### Introduction

The SetProperty method can be used to assign any property on a GraphicalUiElement by the name of the property. This method is used internally to apply states, but it can also be used to perform scripting or assign state values without access to code generated types.

### Code Example - Assigning Position

The following code assigns the Y value of a ColoredRectangle according to keyboard input.

```
void CustomActivity(bool firstTimeCalled)
{
    var keyboard = InputManager.Keyboard;
    if(keyboard.KeyPushed(Microsoft.Xna.Framework.Input.Keys.Up))
    {
        GumScreen.ColoredRectangleInstance.SetProperty("Y", 10.0f);
    }
    if (keyboard.KeyPushed(Microsoft.Xna.Framework.Input.Keys.Down))
    {
        GumScreen.ColoredRectangleInstance.SetProperty("Y", 80.0f);
    }
}
```

<figure><img src="/files/4mpS27e1vxjsiMZujxPd" alt=""><figcaption></figcaption></figure>

### Code Example - Assigning States

SetProperty allows the assignment of states by state name. This is useful when writing code which should apply to any type of Gum object as long as it contains the required category and states. This example assumes a component with a Size category containing two states: Big and Small.

![](/files/ZNOKI71WaHzNe7y5rrbX)

This will automatically generate an enumeration and property in the component:

![](/files/x90ZbvUSJtMZZNnixXTG)

If you are writing code where you do not have access to the specific types, the state category can still be assigned using SetProperty as shown in the following code. Notice that the variable name does not use the prefix "Current", so the variable assigned is simply "SizeState":

```
var keyboard = InputManager.Keyboard;

if (keyboard.KeyPushed(Microsoft.Xna.Framework.Input.Keys.Up))
{
    GumScreen.ComponentWithStatesInstance.SetProperty("SizeState", "Big");
}
if (keyboard.KeyPushed(Microsoft.Xna.Framework.Input.Keys.Down))
{
    GumScreen.ComponentWithStatesInstance.SetProperty("SizeState", "Small");
}
```

<figure><img src="/files/7sRHTvjzIqHK6z0Q5OhI" alt=""><figcaption></figcaption></figure>


---

# 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/flatredball/api/gum-runtime-api/gum-wireframe-graphicaluielement/setproperty.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.
