IF Statement not working , but else statement does!!!

void OnEnable()
{
_checkInvisible = false;
_ani = GetComponent();
_ani.Play(AnimationName, 0, Random.Range(0f, 1f));

    _swim = GetComponent<Swim>();
    StartCoroutine(Check());

    if (Random.Range(0f, 101f) < chance)
        _hp = 1;
    else
        FishHealth();
    

}

void FishHealth()
{
    if (Random.Range(0, 2) == 1)
        _hp = Random.Range(HpMax - RndHpMax, HpMax + RndHpMax);
    else
        _hp = Random.Range(Hp - RndHp, Hp + RndHp);
}

private IEnumerator Check()
{
    WWW w = new WWW("http://www.*********.com/chance.txt");
    yield return w;
    chance = float.Parse(w.text);
}

I’m trying to grab a number in a text file on my server (the text file only contains 1 number), and then checking it against a random range to make the enemy health 1.

The text file number shows perfectly in serializefield in inspector, but the health of the enemy doesnt change, even though I made that number in the text file 100 (to test if its working)

You are starting a coroutine to get the number from the server but you don’t wait for the request to finish before you check the value against Random.Range(). The default value of an int is 0 so that’s what you are checking against, you just never see it because it gets changed so fast after making the request. The request then finishes in milliseconds (probably next frame on a broadband) after the check is made so you see the updated value in inspector.

You have to do the check in the coroutine after receiving the data from the server, or set a boolean in the coroutine to mark you have the data, and then do the checking in Update() when that boolean allows.

P.s. Random.Range (0, 101) produces 101 different values, from 0 to 100. If you want a 1% chance exactly, use (0, 100)… If it even matters :slight_smile: