- Home /
Can I iterate through an enum?
See the below code:
if (pieceName == Piece.PieceType.ANTIDEPRESSANTS.ToString ()) {
return Piece.PieceType.ANTIDEPRESSANTS;
}
There are 10 other types of Piece.PieceType's. Is it possible to use some kind of for loop to flick through each one? Like this, but functional?
for (int i = 0; i<Piece.PieceType.count; ++i){
if (pieceName == Piece.PieceType[i].ToString ()) {
return Piece.PieceType[i];
}
}
I found this and this answer, but I don't have an Enum class, or a GetValues() function. There's got to be some way, right?
Comment
Best Answer
Answer by SirCrazyNugget · Apr 05, 2014 at 02:28 AM
Enum exists in System namespace
string[] PieceTypeNames = System.Enum.GetNames (typeof(PieceType));
for(int i = 0; i < PieceTypeNames.Length; i++){
Debug.Log (PieceTypeNames[i]);
}
Aaaaaaaahhhhhh. Thanks man, I feel dumb now haha.
Works like a charm
Answer by jbat100 · Aug 31, 2017 at 03:52 PM
If you want the enum as opposed to the string representation:
using System;
foreach(Piece.PieceType pieceType in Enum.GetValues(typeof(Piece.PieceType)))
{
Debug.Log (pieceType);
}