0

Hello I'm trying to compare my two UI Text but somehow it wont compare with each other

    if (WordMatch.text == WordGenerator.text) 
    {
        Debug.Log ("Hello");
    }

Here is my code for comparing the two UI Text.

    TextAsset wordText = Resources.Load<TextAsset> ("Words");

    name = wordText.text.Split ("\n" [0]);

    WordGenerator.text = name [Random.Range (0, name.Length)];

And here is the code where I get the value of my "WordGenerator"

Thank you for your time :).

derHugo
  • 83,094
  • 9
  • 75
  • 115
Alex Ryan
  • 3
  • 2
  • What is the problem exactly, are they not comparing correctly? Have you tried to Debug.Log (WordMatch.text) and Debug.Log (WordGenerator.text) to make sure that they really are the same? – Demandooda Jan 06 '19 at 04:56
  • Hello Demandooda thank you for your response. Yeah I've tried doing a Debug.Log with them and (WordGenerator.text) is printing properly the same with (WordMatch.text). But if I used a regular string replacing the (WordGenerator.text) it works fine. – Alex Ryan Jan 06 '19 at 05:02
  • Instead of ==, try `WordMatch.text.Equals (WordGenerator.text)` Look at this issue here: [link](https://stackoverflow.com/questions/814878/c-sharp-difference-between-and-equals) – Demandooda Jan 06 '19 at 05:05
  • Yeah still does not work :/ – Alex Ryan Jan 06 '19 at 05:06

1 Answers1

0

It could have been because of the encoding of your asset file.

You can use this function instead of "==" for comparing

private bool AreEqual(string val1,string val2)
{
    if(val1.Length != val2.Length)
        return false;

    for (int i = 0; i < val1.Length; i++)
    {
        var c1 = val1[i];
        var c2 = val2[i];
        if (c1 != c2)
            return false;
    }

    return true;
}
Hesamom
  • 175
  • 9