If statement unexpectedly changing value.
The update method is supposed to create a zombie for my game every 10 seconds. I just tried to add a random element to it so that it doesn't spawn every time, just randomly. The problem is that the random variable that I created is being changed to less than or equal to one, making it not random at all.
I don't understand why this is happening, I have code just like this all throughout my program and it works just find everywhere except for here. I understand that for some reason it isn't being read as conditional but I have no idea why, it has always worked for me. What am I doing wrong? I am sure that it must be something simple.
I added the whole Update method for reference, but the part that I am talking about is the first part of the if statement, the other stuff doesn't really matter. The variable of random is declared just above it.
void Update () {
float random = Random.Range(0f, numberOfSpawners);
int variable = Mathf.RoundToInt (Time.realtimeSinceStartup % spawnRate);
if (random <=1 && variable == 0f && !alreadySpawned) {
LevelControl();
spawnCount++;
spawnRate -= .1f;
GameObject spawnedZombie = Instantiate(zombie, transform.position, Quaternion.identity) as GameObject;
spawnedZombie.transform.parent = gameObject.transform;
alreadySpawned = true;
}
else if (variable != 0f && alreadySpawned){
alreadySpawned = false;
}
}
No, it is set to 4 at the moment. What is happening is if I print random before the if statement then it gives me a random number in the range selected. However, if I print random inside the if statement then it gives me between 0 and 1. The if statement is changing the value of random to be less than or equal to one.