Compare TextAsset string to String Fail

Hi Guys,

I’m comparing a TextAsset substring to a set string I’ve created. Even when the two appear completely identical, it still does not work. Can anyone explain?

    public bool ShowCard(string cardName)
    {
        int i = 0;

        Debug.Log ("ITS CALLING IT");

        foreach(GameObject card in cards)
        {
            string thisCardName = card.GetComponent<MissionCard>().GetCardName();

            Debug.Log(thisCardName + " vs " + cardName);

            if(thisCardName == cardName)
            {
                Debug.Log("ITS TRUE I LOVE SMALL PEOPLE!!!");
                cards [i].gameObject.SetActive (true);
                activeCards.Add (cards [i].gameObject);
                RepositionCards ();
                cards.Remove (cards [i]);

                return true;
            }
        }

        return false;
    }

However even when it returns the seemingly same this… It never returns the Debug.Log within the if statement.

On calling it I’ve tried:

cardGetter.ShowCard("The High Ground\n");
cardGetter.ShowCard("\nThe High Ground");
cardGetter.ShowCard("\nThe High Ground\n");

None work… Any ideas?

Just so you can see… This is where the TextAsset is created and instantiated…

textDeckName = Resources.Load("DeckName/Regular_Names") as TextAsset;
string[] lineDeckName = textDeckName.text.Split ('\n');
InstantiateCard(i, lineDeck[i], lineDeckName[i]);
//.... stuff
//.... in another function

    void InstantiateCard(int i, string s, string n)
    {
        GameObject missionCard = Instantiate (card, new Vector3 (0, 0, 0), Quaternion.identity) as GameObject;
        missionCard.transform.SetParent(GameObject.Find("CardParent").gameObject.transform, false);
        missionCard.name = "Card_" + i.ToString();
        missionCard.GetComponent<Image>().sprite = (Sprite)missImage[i];
        missionCard.GetComponent<MissionCard> ().SetMissionPoint (lineDeckPoints [i]);
        missionCard.GetComponent<MissionCard> ().SetCardName (n);
        missionCard.GetComponent<MissionCard> ().SetSpecialRequirements(s);
        missionCard.gameObject.SetActive (false);
        cardGetter.addCard(missionCard);
    }

Unless you’re planning on using cards that has the same name, but different casing, using toLowerCase and trim should give you better results:

if(thisCardName.ToLower().Trim() == cardNam.ToLower().Trim()e)

If you want concrete details on exactly how the two strings differ, check their ToCharArray, and print the characters one by one. I’m assuming it’s some kind of white space error - either an extra newline or an extra space. You could print the length of the strings to see if that’s the issue too.