# Random.value sometimes doesn't instantiate my prefab(pic included)

I know its because i’m using the values wrong, but i’ve been playing with them for a while and haven’t been able to find the correct ones.

`using UnityEngine;
using System.Collections;

public class CubeGrid : MonoBehaviour {

``````public Transform grassLand;
public Transform city;

public int xCubes;
public int yCubes;
private int x = 0;
private int y = 0;

void Start () {
while(y < yCubes)
{
if(x == xCubes)
{
y++;
x = 0;
}
while(x < xCubes)
{
if(Random.value <= .8)
{
Instantiate(grassLand, new Vector3(x, y, 0), Quaternion.identity);
}
if(Random.value <= 0.15)
{
Instantiate(dirtRoad, new Vector3(x, y, 0), Quaternion.identity);
}
if(Random.value <= 0.05)
{
Instantiate(city, new Vector3(x, y, 0), Quaternion.identity);
}
{
x++;
}
}
}
}
}`
``````

If somoene could tell me how to properly set up the percentages i’d be thankful.

It’s because you have no conditional for when Random.value is greater than 0.8

Also with your current conditionals, you are instantiating more than one brick sometimes eg : if Random.value < 0.05, you are instantiating all 3 land types. It would be better to start checking at the lowest value, and use else if statements.

Also, Random.value is not carried through all your conditionals, rather a new value is created for each conditional.

I’m curious as to why you are using while loops instead of for loops.

Here is an example combining all my observations :

``````for ( int y = 0; y < yCubes; y ++ )
{
for ( int x = 0; x < xCubes; x ++ )
{
float rnd = Random.value;

if ( rnd < 0.05 )
{
Instantiate(city, new Vector3(x, y, 0), Quaternion.identity);
}
else if ( rnd < 0.15 )
{
Instantiate(dirtRoad, new Vector3(x, y, 0), Quaternion.identity);
}
else // the previous 2 conditions were not met, so just instantiate grass
{
Instantiate(grassLand, new Vector3(x, y, 0), Quaternion.identity);
}
}
}
``````