- Home /
Enum to int issue
I am attempting to put my character generator together. I'm pulling the enum values from the attributes script and want to display them as a string. The error that comes up is- cannot convert type to int. The more I try to fix it, the more it come up with problems.
for(int ctr = 0; ctr < Enum.GetValues(typeof(Attribute)).Length; ctr++){
GUI.Label(new Rect(10, 40+(ctr * 25), 100, 25), ((Attribute)ctr).ToString());
}
What am I doing wrong? Thanks!
Answer by syclamoth · Mar 13, 2012 at 12:45 AM
You need to cast the 'Enum.GetValues' back to an int:
for(int ctr=0; ctr < (int)Enum.GetValues(typeof(Attribute)).Length; ctr++){
GUI.Label(new Rect(10, 40 + (ctr * 25), 100, 25), ((Attribute)ctr).ToString());
}
Thanks! I must have missed something along the way because I got another error of the same type. Assets/Scenes/CharacterGenerator.cs(21,67): error CS0030: Cannot convert type int' to
System.Attribute'
Also getting(maybe old) Assets/Scenes/CharacterGenerator.cs(21,9): error CS1502: The best overloaded method match for UnityEngine.GUI.Label(UnityEngine.Rect, string)' has some invalid arguments and Assets/Scenes/CharacterGenerator.cs(21,9): error CS1503: Argument
#2' cannot convert object' expression to type
string' Having a very hard time with this...
It's because you went and named your enum 'Attribute'. Call it something different- it's getting confused between your enum, and the System.Attribute class.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Cast the result of an enum as a string 3 Answers
Default Editor : Enum as flags ? 8 Answers
How to assign Enums to button? 0 Answers