If statement not working correctly
Hallo everybody, just wrote a piece of code and it seems my if statement is not working as I expect. The randomizer(n) is a function which returns true if n%randInt==0;
The problem is that the Spawn function keeps spawning objects. It gets the right _ObjOnScreen value but doesnt break/continue if the value > 2. Seems like it just skips the if statement and keeps spawning.
if (_ShieldsOnScreen <= 2 && Randomizer(120)) { Instantiate(_PowerUps_Prefab[0], new Vector3(Random.Range(-9.0f, 10.0f), 6, 0), Quaternion.identity); _ShieldsOnScreen++; }
if (_SpeedOnScreen <= 2 && Randomizer(80)) { Instantiate(_PowerUps_Prefab[1], new Vector3(Random.Range(-9.0f, 10.0f), 6, 0), Quaternion.identity); _ShotsOnScreen++; }
if (_ShotsOnScreen <= 2 && Randomizer(40)) { Instantiate(_PowerUps_Prefab[2], new Vector3(Random.Range(-9.0f, 10.0f), 6, 0), Quaternion.identity); _SpeedOnScreen++; }
Please help.
if cant be the problem, can youdebug.log all the _shieldPmScreen _speedonscreen and _shotonscreen value?
Hi, thank you for your reply. Yes I did debug.log the values. The if statement is failing cause the values are higher than 2 and it keeps spawning. If I use small numbers, it fails, if I use greater numbers, so the chance of (x%y==0) gets lower, it's working properly.