- Home /
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 dirtRoad; 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.
your logic is very wrong actually... if the random value is less than 0.05 you are instantiating 3 objects, is this wat u intend to do?
also the reason for some of the spots to be empty is because the value is greater than or equal to 0.8.
I intend to instantiate more than 3 eventually actually. I know my values are already wrong I'm just not sure how to do it. The unity doc page for random.value is very small.
Answer by AlucardJay · Feb 17, 2014 at 06:03 AM
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);
}
}
}
Thanks man this is what i was trying to do but way more efficent and actually functional.
Just saw the comments added while typing this. The above method works for extra tiles, just follow the same pattern :
if ( rnd < 0.05 )
{
Instantiate ...
}
else if ( rnd < 0.15 )
{
Instantiate ...
}
else if ( rnd < 0.25 )
{
Instantiate ...
}
else if ( rnd < 0.45 )
{
Instantiate ...
}
else if ( rnd < 0.65 )
{
Instantiate ...
}
else // the previous conditions were not met, so just instantiate grass
{
Instantiate ...
}
Your answer
Follow this Question
Related Questions
Instantiate a random prefab at an objects location 3 Answers
Randomly pick then create prefab 2 Answers
How to access property of a prefab before Instantiating. 1 Answer
Instantiate issues 1 Answer
Prefab Instantiate 0 Answers