- Home /
IF Statement not working , but else statement does!!!
void OnEnable()
{
_checkInvisible = false;
_ani = GetComponent<Animator>();
_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)
I narrowed down the problem a little , when I put
if (Random.Range(0f, 101f) != chance)
it works, but the < doesnt?
I am definitely no expert, but maybe the problem is that you're using Random.Range()
with floats? just give it a try and see what happens...
I tried change all values to ints and it pretty much does the same, I added them to the inspector to check and indeed they do show up
The problem is that if statement doesn't seem to work if I use f < chance ... probably a really simple fix I'm overlooking
What do you mean with F? It's not used in the code in question. Nevertheless, using a chance of 100 against a range of 0 to 101 gives a chance less than 1%.
When do you expect this to happen? How did you confirm that it's doesn't work?
Hey, This is strange...If you are using Visual Studio you can look up how this piece of code compiles to IL and see if there any anomalies.
I'm not sure why but I think it has to do with Coroutine now.. because I tested and made chance = 100; in the OnEnable function and it always works, but when I use the StartCoroutine It doesn't work ...
Answer by NoseKills · Sep 17, 2017 at 09:28 PM
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 :)
Thanks ! Didn't realize that the OnEnable function was firing so fast , I added everything below StartCoroutine to the IEnumerator and now it works perfectly!! :)
I'm making a casino type game and now I can change the chance value anytime I want even if users are playing the game ins$$anonymous$$d of making them download an updated apk file in case I made the chance too high or too low .. I'm probably doing it the super noob way because I'm pretty new to unity , but it's still amazing :)
Your answer
