How would I use Random.Range and convert the result to a private float, which is used elsewhere in the script to determine a value? The values in this case would be delay1 and delay2. I am self taught and still learning, so please explain simply.
IEnumerator EnableLight()
{
while(true)
{
yield return new WaitForSeconds(delay1);
lightOn = true;
Base1.gameObject.SetActive(false);
Base2.gameObject.SetActive(true);
yield return new WaitForSeconds(delay2);
lightOn = false;
Base1.gameObject.SetActive(true);
Base2.gameObject.SetActive(false);
}
}
If delay1 and delay2 are both floats then Random.Range
will return a float for you automatically and you don’t need to read any more.
The rest of this assumes that delay1 and delay2 are both integers. If delay1 and delay2 are both integers, then RandomRange
will return an Integer for you in the range starting from delay 1 but excluding delay2.
You need to think about what you want. Do you want the answer to include part seconds or only whole seconds. If it’s part seconds then you need to convert delay1 and delay2 to floats first. If you are happy with whole seconds but still need a float then you convert the result to float, not the inputs.
Converting to a different data type is called “casting” and to cast from int to float you use
someFloatNumber = (float)someIntegerNumber;
That means you want one of the following two options:
float answer = Random.Range((float)delay1, (float)delay2);
float answer = (float)Random.Range(delay1, delay2);