I am trying to create a switch statement based on a two character string. However, this does not seem to work and always evaluates to the default value. So, can someone please show me the correct way to do this?
Here is my (supposedly incorrect) code:
string GetPieceCode(GameObject piece)
{
string pieceCode = "";
Debug.Log(piece.gameObject);
pieceCode = piece.ToString().Remove(2,2);
Debug.Log(pieceCode);
return pieceCode;
}
Debug.Log(pieceType);
switch (pieceType)
{
case "BP":
Debug.Log("Black Pawn Selected");
break;
case "WP":
Debug.Log("White Pawn Selected");
break;
case "WB":
print("White Bishop Selected");
break;
case "BB":
print("Black Bishop Selected");
break;
case "WK":
print("White Knight Selected");
break;
case "BK":
print("Black Knight Selected");
break;
case "WR":
print("White Roook Selected");
break;
case "BR":
print("Black Rook Selected");
break;
case "WKing":
print("White King Selected");
break;
case "WQueen":
print("White Queen Selected");
break;
case "BKing":
print("Black King Selected");
break;
case "BQueen":
print("Black Queen Selected");
break;
default;
debug.log("Error");
break;
}
The source Debug.Log(pieceType)code outputs a 2-character string code that indicates that it matches case values. Therefore, I do not see what is going wrong.
In addition, print()and are Debug.Log()identical in this context.
source
share