# GetInt

> Returns the value corresponding to key in the preference file if it exists.

## Definition

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

## GetInt(string, int)

Returns the value corresponding to `key` in the preference file if it exists.

```csharp
public static int GetInt(string key, int defaultValue)
```

### Parameters

**** (\[string]\(https\://learn.microsoft.com/dotnet/api/system.string)): Name of key to read integer from.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): Integer value to return if the key is not in the storage.

### Returns

| Type                                                       | Description                              |
| ---------------------------------------------------------- | ---------------------------------------- |
| [int](https://learn.microsoft.com/dotnet/api/system.int32) | The value stored in the preference file. |

### Remarks

If the value doesn't already exist in the preference file the function will return `defaultValue`.

Additional Resources: [EditorPrefs.SetInt](/engine/6000.6/script-reference/unityeditor/editorprefs/setint.md).

### Examples

```csharp
// A small editor window that allows an integer value to be
// read and written to the EditorPrefs online storage.
//
// SetIntExample is the name of the int to read/write.

using UnityEngine;
using UnityEditor;

public class ExampleClass : EditorWindow
{
    int intValue = 42;

    [MenuItem("Examples/Prefs.GetInt Example")]
    static void Init()
    {
        ExampleClass window = (ExampleClass)EditorWindow.GetWindow(typeof(ExampleClass));
        window.Show();
    }

    void OnGUI()
    {
        int temp;
        temp = EditorPrefs.GetInt("SetIntExample", -1);
        EditorGUILayout.LabelField("Current stored value: " + temp.ToString());
        intValue = EditorGUILayout.IntField("Value to write to Prefs: ", intValue);
        if (GUILayout.Button("Save value: " + intValue.ToString()))
        {
            EditorPrefs.SetInt("SetIntExample", intValue);
            Debug.Log("SetInt: " + intValue);
        }
    }
}
```

## GetInt(string)

Returns the value corresponding to `key` in the preference file if it exists.

```csharp
public static int GetInt(string key)
```

### Parameters

**** (\[string]\(https\://learn.microsoft.com/dotnet/api/system.string)): Name of key to read integer from.

### Returns

| Type                                                       | Description                              |
| ---------------------------------------------------------- | ---------------------------------------- |
| [int](https://learn.microsoft.com/dotnet/api/system.int32) | The value stored in the preference file. |

### Remarks

If the value doesn't already exist in the preference file the function will return `defaultValue`.

Additional Resources: [EditorPrefs.SetInt](/engine/6000.6/script-reference/unityeditor/editorprefs/setint.md).

### Examples

```csharp
// A small editor window that allows an integer value to be
// read and written to the EditorPrefs online storage.
//
// SetIntExample is the name of the int to read/write.

using UnityEngine;
using UnityEditor;

public class ExampleClass : EditorWindow
{
    int intValue = 42;

    [MenuItem("Examples/Prefs.GetInt Example")]
    static void Init()
    {
        ExampleClass window = (ExampleClass)EditorWindow.GetWindow(typeof(ExampleClass));
        window.Show();
    }

    void OnGUI()
    {
        int temp;
        temp = EditorPrefs.GetInt("SetIntExample", -1);
        EditorGUILayout.LabelField("Current stored value: " + temp.ToString());
        intValue = EditorGUILayout.IntField("Value to write to Prefs: ", intValue);
        if (GUILayout.Button("Save value: " + intValue.ToString()))
        {
            EditorPrefs.SetInt("SetIntExample", intValue);
            Debug.Log("SetInt: " + intValue);
        }
    }
}
```
