- Home /
How would you convert string to enum
The following code works which was used for testing;
myTarget.category = Info.Category.Planet; //Works, popup changes to Planet on Info script.
Now I wanted to work automatically, and this did not work, but gives the same result.
print(Category[index]); // The result is Planet. Ok, lets replace and paste it in.
myTarget.category = Info.Category.Category[index]; // OK, didn't work
This EditorGUILayout.Popup was made as a string, how would you convert this to enum? Thx.
Comment
Best Answer
Answer by roojerry · Apr 18, 2014 at 08:29 PM
Try Enum.Parse
yeah I tried that, but I could fit it right into place.
This is what they have
PetType pet = (PetType)Enum.Parse(typeof(PetType), value);
and I tried to convert it to my usage in which it didn't work.
myTarget.category = (myTarget.category)Enum.Parse(typeof(myTarget.category), Category[index]);
It is going from my unity editor to my info script which has an enum popup list.
public class Info : $$anonymous$$onoBehaviour
public enum Category
{
Asteroid,
Galaxy,
Planet,
Satellite,
Star
}
public Category category;
// Here is the UnityEditorScript
string[] Category = new string[]{ "Asteroid", "Galaxy", "Planet", "Satellite", "Star"};
index = EditorGUILayout.Popup("Category:", index, Category);
Info.Category home = Info.Category.Star; //Works, but Star needs to be replaced with .Category[index].
Your answer
Follow this Question
Related Questions
Cast the result of an enum as a string 3 Answers
Converting enum to string ? 1 Answer
Get JSON array object string value 2 Answers
iOS Not Converting String to Integer 0 Answers
Convert String To Class 1 Answer