- Home /
Selecting a number within Random.Range
Hi everyone, so I have certain textures I want to enable when Random.Range picks a number from 0-5. The thing is that my superNumberButton which has the Random.Range, constantly updates and not stop and choose a number. As you can see the script updates the superNumberButton constantly without stoping and as soon as energy = 3 it will show every texture. I also tried putting the superNumberButton in the Start function but the thing is that it will select a number but it won't update to generate a new number. How am I able to make my superNumberButton select a number, and then update and select again when the time is right?
void Update(){
superNumberButton = Random.Range(0,6);
if (energy == 3) {
switch (superNumberButton) {
case 0:
blazeCannonTexture.enabled = true;
isBlaze = true;
break;
case 1:
multiplierTexture.enabled = true;
isMulti = true;
break;
case 2:
collectorTexture.enabled = true;
isCollect = true;
break;
case 3:
unifiedShieldTexture.enabled = true;
isShield = true;
break;
case 4:
guidedMissleTexture.enabled = true;
isMissle = true;
break;
case 5:
nukeTexture.enabled = true;
isNuke = true;
break;
}
}
}
Answer by alok-kr-029 · Feb 27, 2015 at 11:16 AM
your question is not soo clear but hope you mean somthing like this
bool rightTime; void Update(){
if(rightTime)
superNumberButton = Random.Range(0,6);
if (energy == 3) {
switch (superNumberButton) {
rightTime= false;
case 0:
blazeCannonTexture.enabled = true;
isBlaze = true;
break;
case 1:
multiplierTexture.enabled = true;
isMulti = true;
break;
case 2:
collectorTexture.enabled = true;
isCollect = true;
break;
case 3:
unifiedShieldTexture.enabled = true;
isShield = true;
break;
case 4:
guidedMissleTexture.enabled = true;
isMissle = true;
break;
case 5:
nukeTexture.enabled = true;
isNuke = true;
break;
}
}
}
Now trigger the rightTime when ever you want to recharge
Thanks I would have never though about making a bool for this. Awesome thanks a bunch.
Answer by Graham-Dunnett · Feb 27, 2015 at 11:12 AM
Somewhere in your code you need to watch over time, and only do your current code when time is right.
myTime += Time.deltaTime;
if (myTime > importantTimeValue) {
myTime = 0.0f; // start from the beginning again
if (energy == 3) {
superNumberButton = Random.Range(0,6);
// and the rest of your switch code
}
}
There's no point doing any of your code until the time is right, and when the time is right, you might as well check what energy is before you do any thing further.