# Enum value from String

**URL:** https://discussions.unity.com/t/enum-value-from-string/644373
**Category:** Scripting
**Tags:** Scripting
**Created:** [October 28, 2016, 9:32pm UTC](https://discussions.unity.com/t/enum-value-from-string/644373 "2016-10-28T21:32:31Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Laminant](https://avatars.discourse-cdn.com/v4/letter/l/cab0a1/32.png) [@Laminant](https://discussions.unity.com/u/Laminant)
#### Post date: [October 28, 2016, 9:32pm UTC](https://discussions.unity.com/t/enum-value-from-string/644373/1 "2016-10-28T21:32:31Z")

</div>

I’m trying to get the uint value from an enum when the input value I have is a String.

So for:

public enum colors{red, blue, green};  
public uint chosenColorCode;  
public string chosenColor = “blue”;

void Start(){  
chosenColorCode = colors.chosenColor; //Does not work, but how would I get the color code from the string?  
}

---

<div class="post-metadata">

### Author: ![makeshiftwings](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/makeshiftwings/32/334244_2.png) [@makeshiftwings](https://discussions.unity.com/u/makeshiftwings)
#### Post date: [October 28, 2016, 9:37pm UTC](https://discussions.unity.com/t/enum-value-from-string/644373/2 "2016-10-28T21:37:36Z")

</div>

Try this:

```auto
chosenColorCode = (uint) Enum.Parse(typeof(colors), chosenColor);

```

---

<div class="post-metadata">

### Author: ![Laminant](https://avatars.discourse-cdn.com/v4/letter/l/cab0a1/32.png) [@Laminant](https://discussions.unity.com/u/Laminant)
#### Post date: [October 28, 2016, 10:00pm UTC](https://discussions.unity.com/t/enum-value-from-string/644373/3 "2016-10-28T22:00:16Z")

</div>

> [@makeshiftwings](#):
>
> Try this:
> 
> ```auto
> chosenColorCode = (uint) Enum.Parse(typeof(colors), chosenColor);
> 
> ```

The output says:  
“InvalidCastException: Cannot cast from source type to destination type.”

---

<div class="post-metadata">

### Author: ![MV10](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/mv10/32/453727_2.png) [@MV10](https://discussions.unity.com/u/MV10)
#### Post date: [October 28, 2016, 10:16pm UTC](https://discussions.unity.com/t/enum-value-from-string/644373/4 "2016-10-28T22:16:41Z")

</div>

I think you have to cast it to int first, then to uint. If I remember right they’re ints but boxed so they look like objects.

```auto
chosenColorCode = (uint) (int) Enum.Parse(typeof(colors), chosenColor);

```

This will probably work:

```auto
chosenColorCode = Convert.ToUInt32(Enum.Parse(typeof(colors), chosenColor);

```

---

<div class="post-metadata">

### Author: ![Laminant](https://avatars.discourse-cdn.com/v4/letter/l/cab0a1/32.png) [@Laminant](https://discussions.unity.com/u/Laminant)
#### Post date: [October 28, 2016, 10:32pm UTC](https://discussions.unity.com/t/enum-value-from-string/644373/5 "2016-10-28T22:32:13Z")

</div>

That works! Thank you so much guys!
