- Home /
C20019 error with '>=' cannot be applied to operands 'string' and string'
I'm trying to make it so that when my static ball8 gets hit by a moving ball(ball), and the count is greater than or equal to 40, a yellow wall changes to red.
For some reason I get the error with && countText.text >= "40"..... but if I put && countText.text == "40", then everything is fine. Its just the '>' that messes everything up.
Any help would be appreciated. Thanks!
if (ballCollideBall8.gameObject.tag == "ball" && countText.text >= "40")
{
//Ball8.SetActive (false);
LeftYellowSideWall.GetComponent<LeftYellowWall> ().ColorChangeRed1();
Ball8.GetComponent<MeshRenderer>().enabled = false;
Ball8.GetComponent<CircleCollider2D> ().enabled = false;
ball8Green = false;
}
}
Answer by Elarm00 · Jun 22, 2017 at 11:57 AM
You are trying to use '>=' operator on strings, which is not defined. You should be comparing numbers not literals. So first thing to do is convert your string to int. Ex.
...
int count = Convert.ToInt32(countText.text);
if (ballCollideBall8.gameObject.tag == "ball" && count >= 40)
...
The reason why '==' operator works fine is because it`s actualy defined, so you can compare strings, like:
"aaa" == "aaa" - true "aaa" == "asa" - false
Thanks for the reply!
I tried putting the..."int count = Convert.ToInt32(countText.text);" in the void Start() above that code and the "Convert.ToInt32" ended up going red. Where should I be placing it?
Never $$anonymous$$d I got it to work! Didn't have "using System" at the top. And then I placed the code just above the if statement like you have it.
Thanks a lot!
You really should be storing your ball count out of a string, in it's own integer not converting a string to an int. You can update your text using your int.
Yeah I'm going to try and adopt that habit for future projects. I'm only a month in on Unity so I'm still working out the learning kinks. Thanks!