Hie everyone!
I want to create a cube, but just when the random.range(0,1) is 1. When it is 0, then nothing comes
Thank you and sorry for my bad english!
var cube : GameObject;
var spawnPosition : GameObject;
var timer = 0.0;
function SpawnCube() {
var tempCubeSpawner = Instantiate(cube, spawnPosition.transform.position, Quaternion.identity);
}
function Update() {
timer += Time.deltaTime;
if(timer > 5) {
var randomNumber = Random.Range(0,1);
if(randomNumber == 0) {
SpawnCube();
timer = 0.0;
}
if(randomNumber == 1) {
timer = 0.0;
}
}
}
I’m assuming your randomisation is intended to be a coin toss, in
other words you’re wanting it to be an int that’s either 0 or 1.
With ints, Random.Range is exclusive of the second (max) parameter.
So if you want one of 0 and 1 you need to do
var randomNumber = Random.Range(0,2);
Or you could use floats and you could do
var randomNumber = Random.Range(0f,1f);
But then the test changes to
if (randomNumber < 0.5f)
{
}
else
{
}
The advantage of floats is that it gives you an easy way to
tweak things, by changing the 0.5f to a variable you can set
to whatever probability you want.
Note that when using floats Random.Range is inclusive (but
it makes negligible difference when using it this way).