4

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.

13
  • Are you sure that it's printing an identical two character code? Any whitespace after it etc perhaps? Commented Nov 12, 2015 at 20:45
  • What is the exact value of pieceType at runtime? switch statements can use strings without any problem. Commented Nov 12, 2015 at 20:46
  • What typ,e of variable is pieceType? Maybe you need pieceType.ToString() Commented Nov 12, 2015 at 20:46
  • 2
    What if you try something simple like Debug.Log(pieceType == "BP") or Debug.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 BP Commented Nov 12, 2015 at 20:47
  • 1
    @JamesHughes: Don't just look at the rendered printable string. There could be any number of non-printable characters there. When you debug, convert the pieceType string into a character array and see what's in that array. Clearly there's something different from the expected values. Commented Nov 12, 2015 at 20:50

1 Answer 1

1

The problem lies within the face that I assumed the GameObject piece would return only the 3 character name assigned to it within Unity. Thus, I assumed that to get the first 2 for the code I would only have to remove the first digit.

However, all of my debugging attempts were compromised by the fact that I thought (incorrectly) that BP(UnityEngine.GameObject) as an output was merely BP as a string with some added debug information.

The correct method of generating the piece code is:

string GetPieceCode(GameObject piece)
    {
        string pieceCode = "";
        Debug.Log(piece.gameObject);
        pieceCode = piece.ToString().Substring(0,2);
        Debug.Log(pieceCode);
        return pieceCode;
    }

Thanks for everyone's help.

Edit: Although not part of the original problem, I changed the codes for Kings and Queens to B|Wk and B|Wq respectively, making use of the case sensitivity.

Sign up to request clarification or add additional context in comments.

2 Comments

How do you know if it's a King or Queen if you're using Substring(0,2)?
@sab669 thanks for the help, learning a new engine and a new language at the same time is driving me up the wall.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.