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?
}
Try this:
chosenColorCode = (uint) Enum.Parse(typeof(colors), chosenColor);
The output says:
“InvalidCastException: Cannot cast from source type to destination type.”
MV10
4
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.
chosenColorCode = (uint) (int) Enum.Parse(typeof(colors), chosenColor);
This will probably work:
chosenColorCode = Convert.ToUInt32(Enum.Parse(typeof(colors), chosenColor);
That works! Thank you so much guys!