If statement for GUIText?:

I have a guitext that displays a 4 digit number and I am trying to compare it to another 4 digit number like this -

/
/
if(Blah.guiText.text > 2000)
        {
        Blah balh balh;
        }
/
/

But it is giving me this-

/
/
error CS0019: Operator `>' cannot be applied to operands of type `string' and `int'
/
/

I tried using the “” on the number but then it just gives me the same error about a method. I thought both of these were strings so this would work.

What am I doing wrong??

If your GUI text is a number, you need to parse it first.

int value;
if (int.TryParse(Blah.guiText.text, out value))
{
   if (value > 2000)
   {
      Blah blah blah;
   }
}
else
{
   // The number could not be parsed.
   Debug.Log("Could not parse integer.");
}

You can use int.Parse to turn a string into a number, but if it fails, it will throw an exception, which will stop your game. If you use int.TryParse, you can stop that from happening. If the TryParse fails, you can warn the Player or take some action to correct the problem and then try again.

Numbers cannot be compared to strings unless they are brought to a common type for comparison. Convert your string value to a number and then compare.

var blahblahnumval = Convert.ToInt32(Blah.guiText.text);
if(blahblahnumval  > 2000)
        {
        Blah balh balh;
        }

EDIT: Also what EddyEpic says… (beat me by seconds)

Thanks guys, it is the same in .net…DUH

I’ll let you know how it goes in a few minutes.

BPPHarv,

How would I do this in C#…I tried to convert it but it isn’t doing it…

I got it working, thanks a lot guys.

The convert function is a part of the System namespace so you’ll need to include it at the start of the c# file.

using System;

int MyBlahBlahVar = Convert.ToInt32(Blah.guiText.text);

Keep in mind that EddyEpic’s solution would be more valid if the variable being tested can possibly contain anything that isn’t a numeric related character in the string. Mine does no testing what so ever and will likely go bork if there’s any funny character stuff in the numeric string.

Yes Eddys worked right off the bat…But I appreciate you guys taking the time to explain it to me… I have done this before with .net and it did not click till after the fact.

Is it possible to do something like this?

/
/
if(value = Random.Range(1000, 1500)
{
blah blah blah;
}
/
/

I tried it but I get no response from it.

Anyone have any input on the above code about random.range???

This is .net stuff.

Probably because that code is meaningless… what are you trying to do?

If the “value” is whatever it is…can I do a random.range???

Did you read any of the posts before the last couple??

Right now I am doing the value <> then a value, like 1000 or 3000, but I want to do it for the range of 1000-2000 instead of just < then one value.

if (value > 1000  value < 2000)
{
    // do stuff
}

Thanks Kelso