Need help figuring out why my if statement won't pass in Unity c#

Vegard Stenberg :

I'm making a multiplayer game and this is for the ranking list at the end. The wierd thing is that the if-statement dosn't pass eventhough when I print gmD and name they are the same. Any suggestions?

void Awake()
{
    print("scoreBoard");

    foreach (string p in GManageer.deadPlayers)
    {
        scoreString += p + "\n";
    }
    scoreText.text = scoreString;

    string gmD = GManageer.winner;
    string name = controllsManager.username;

    print(gmD + ":gmD");
    print(name + ":name");

    if (gmD == name)
    {
        print("In here too");
        scoreText.text = "Yes, you won!";
        PlayerPrefs.SetInt("XP", PlayerPrefs.GetInt("XP") + (int)(70 / PlayerPrefs.GetInt("Lvl")));
    }
}
leo Quint :

Yes most likely the 2 strings aren't exactly the same. When comparing strings you can use the string.Equal() function to add comparison settings.

if(gmD.Equals(name, StringComparison.InvariantCultureIgnoreCase))

Compare strings using culture-sensitive sort rules, the invariant culture, and ignoring the case of the strings being compared.

Also a small thing to note is using name for your variable name can lead to some bugs since UnityEngine.Object.name is a property.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=390757&siteId=1