Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error CS0019: Operator '<' cannot be applied to operands of type 'string' and 'int'

error CS0019: Operator '<' cannot be applied to operands of type 'string' and 'int'

I tried some ways to fix this, but this is an error yet. Can anyone help me?

public GameObject Xvreli = null;
public int zPos;
public int enemyCount;

void Start()
{
    StartCoroutine(EnemyDrop());
}

IEnumerator EnemyDrop()
{
    while (Xvreli.name < 5)
    {
        zPos = Random.Range(0, 1000);
        Instantiate(Xvreli, new Vector3(0, 0, zPos), Quaternion.identity);
        yield return new WaitForSeconds(0.3f);
        Xvreli.name += 2;
    }

}
like image 588
Beso Jgharkava Avatar asked Jan 30 '26 22:01

Beso Jgharkava


1 Answers

Well the error is quite self-explanatory..

It looks like what you wanted to do is parsing the name value to an int value and back e.g. using int.Parse

int nameInt;
while((nameInt = int.Parse(Xvreli.name)) < 5)
{
    ...

    Xvreli.name = (nameInt + 2).ToString();
}

or also int.TryParse (doesn't throw format exception)

while (int.TryParse(Xvreli.name, out var nameInt) && nameInt < 5)
{
    ...

    Xvreli.name = (nameInt + 2).ToString();
}
like image 180
derHugo Avatar answered Feb 01 '26 12:02

derHugo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!