my code:
when game play some times one object create not other. Please solve this problem . Thanks`
int randomChance = Random.Range(1,10);
Vector3 poss = new Vector3(pos.transform.position.x + Random.Range(-2f, 2f), pos.transform.position.y, pos.transform.position.z);
if(randomChance<7)
{
Instantiate(cube[0], poss, Quaternion.identity);
}
else
{
Instantiate(cube[1], poss, Quaternion.identity);
}
Please, format your code in the forum (select the code and press the “101010” button in the editor toolbar). Otherwise, it’s difficult to read.
However, it looks like you’re choosing a number between 1 and 9 (the INT version of Random.Range is exclusive of the max value). Then, you create one object if the value is < 7 and the other object if it’s not.
So, object 1 gets created if the value is any of: 1, 2, 3, 4, 5, 6
And object 2 gets created if the value is any of: 7, 8, 9
With that in mind, object #1 has twice the odds of being created as object #2. Assuming you want both objects to have the same chance of creation, the odds need to be the same for both.
You could do that a number of ways, but the simplest might be to have your Random.Range call return either a 0 or 1, and use that as the array index to instantiate you object. So, something like:
int randomChance = Random.Range(0, 2); // choose either 0 or 1
...
...
Instantiate(cube[randomChance], poss, Quaternion.identity);