Comparing 2 string but giving me the wrong interger

Script A
I have 2 public string

public string[] Word;
public string[] Color;

There have been added following string, though the Inspector
• Black, Blue, Brown, Green, Orange, Pink, Purple, Red, White, Yellow
In that order and in both of the strings.

Script B
When OnMouseDown action, it gonna send a string Array to Script A, this array is 2 long.
f.x it could contain → Green, Yellow

Script A
I have this code in Script A

void BoxClicked(string[] boxInfo)
    {

        // For Loops Doesn't work properly
        for (int i = 0; i<Word.Length; i++) {
        Debug.Log(i+" Word in array: "+Word[i]+"   Color in Array: "+Color[i]);


            if(string.Compare(boxInfo[0],Word[i]) == 1)

              ClickedWord = i;

            if(string.Compare(boxInfo[1],Color[i]) == 1)
              ClickedColor = i;

        }

        Debug.Log ("Clicked Word: " + boxInfo [0] + " (" + ClickedWord + ") ("+Word[ClickedWord]+") Clicked Color: " + boxInfo [1] + " (" + ClickedColor + ") ("+Color[ClickedColor]+")");

    }

What the script is gonna do is to compare the boxInfo string with Word[ ] and Color[ ]
So when the input parameter to boxInfo is Green, Yellow

We gotta count on that ClickedWord = 3, and ClickedColor = 9
But it doesn’t give me that out.

Here is the Debug.Log test:


0 Word in array: Black Color in Array: Black
1 Word in array: Blue Color in Array: Blue
2 Word in array: Brown Color in Array: Brown
3 Word in array: Green Color in Array: Green
4 Word in array: Orange Color in Array: Orange
5 Word in array: Pink Color in Array: Pink
6 Word in array: Purple Color in Array: Purple
7 Word in array: Red Color in Array: Red
8 Word in array: White Color in Array: White
9 Word in array: Yellow Color in Array: Yellow

Clicked Word: Green (2) (Brown) Clicked Color: Yellow (8) (White)


I really dont that it doesn’t come out as
Clicked Word: Green (2) (Green) Clicked Color: Yellow (8) (Yellow)

Could you help me sorter it out?

You have a free line in line 11. That will cause the if statement in line 10 to not work on the statement in line 12.
For better workflow you should do it like this:

 if(string.Compare(boxInfo[0],Word[i]) == 1){
              ClickedWord = i;
             }
            if(string.Compare(boxInfo[1],Color[i]) == 1){
              ClickedColor = i;
            }

not sure it fixes your problem but it did look like something that could cause problems.

Hey fffMalzvier,
THank you for your answer,
The code part look like this now

void BoxClicked(string[] boxInfo)
    {

         // For Loops Doesn't work properly
        for (int i = 0; i<Word.Length; i++) {
        Debug.Log(i+" Word in array: "+Word[i]+"   Color in Array: "+Color[i]);


            if(string.Compare(boxInfo[0],Word[i]) == 1){

               ClickedWord = i;
            }
            if(string.Compare(boxInfo[1],Color[i]) == 1){
               ClickedColor = i;
            }
        }

        Debug.Log ("Clicked Word: " + boxInfo [0] + " (" + ClickedWord + ") ("+Word[ClickedWord]+") Clicked Color: " + boxInfo [1] + " (" + ClickedColor + ") ("+Color[ClickedColor]+")");

    }

But it didn’t solved the problem

Here is the Debug.Log test:


0 Word in array: Black Color in Array: Black
1 Word in array: Blue Color in Array: Blue
2 Word in array: Brown Color in Array: Brown
3 Word in array: Green Color in Array: Green
4 Word in array: Orange Color in Array: Orange
5 Word in array: Pink Color in Array: Pink
6 Word in array: Purple Color in Array: Purple
7 Word in array: Red Color in Array: Red
8 Word in array: White Color in Array: White
9 Word in array: Yellow Color in Array: Yellow

Clicked Word: Yellow (8) (White) Clicked Color: Brown (1) (Blue)


You’re doing a string.Compare and checking it against ‘1’ (string2 is greater than string1). You need to compare them against 0 to check for a match.

Also, ‘Color’ is a type so it would make more sense to call it ‘ColorList’ or something.

1 Like

Are you sure you aren’t trying to figure out if the strings are equal? You could call ToLower() on each and just do a direct comparison with ==.

1 Like

Thank you Tonemcbride, that solved the problem.

Could you please explain to me perhaps with more details why i should compare it to 0 than 1?

What @DonLoquacious writes.
String.Compare is usually used to sort strings, not check if they are equal.

1 Like

Could also do:

boxInfo[0].Equals(Word[i])

but unlike some other languages, C# handles string comparisons pretty well just using the normal “==” like any other value. If you want to be anal, use .Trim() and .ToLower() on each value as you compare them:

boxInfo[0].Trim().ToLower() == Word[i].Trim().ToLower()

but it really depends on the context of the problem. In this case, you know exactly what the values should be, so there’s no point.

If you want, you can make this 10x more reliable and easier to read by just making a new Enum that corresponds to the string and numerical values that you want. Then you could convert the index to the enum and back again with very simple casting.