I am attempting to create a switch statement based upon a two character string. However, this doesn't seem to work and always evaluates to the default. So, could someone please show me the correct way to do it?
Here is my (presumably 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 initial Debug.Log(pieceType) prints out a 2 character string code which shows that it matches the case values. So I don't see what is going wrong.
Also, print() and Debug.Log() are identical in this context.
pieceTypeat runtime?switchstatements can use strings without any problem.Debug.Log(pieceType == "BP")orDebug.Log(pieceType.Equals("BP"))? Does that evaluate to true? If not, then the characters in the string aren't quite what they appear. Maybe there's a carriage return on the end, or a space, or they're some random other language character that just so happens to look like BPpieceTypestring into a character array and see what's in that array. Clearly there's something different from the expected values.