- Home /
Random.Range produces 2 numbers for me?
Hi. I'm making a game, you're a car, and you need to avoid walls to get to the finish line. The walls are randomly picked from prefabs i've already made, so the track will be different every time you play. I'm having a problem though, were every 10 blocks, were one prefab should spawn, 2 are spawning. I know the reason 2 are spawning, it's because the random.range values that pick which prefab to pick are setting it equal to 2 numbers somehow, and i have it if it's equal to the number of that prefab, it will create that. For example, i printed the value of the randRoad1 variable, and I get 2 numbers in the console. How is this happening? Here's my script var road1 : GameObject; var road2 : GameObject; var road3 : GameObject; var road4 : GameObject; var road5 : GameObject; var road6 : GameObject; var road7 : GameObject; var road8 : GameObject; var road9 : GameObject; var road10 : GameObject; var road11 : GameObject; var road12 : GameObject; var road13 : GameObject; var road14 : GameObject;
var randRoad1 : int;
var randRoad2 : int;
var randRoad3 : int;
var randRoad4 : int;
var randRoad5 : int;
var randRoad6 : int;
var randRoad7 : int;
var randRoad8 : int;
var randRoad9 : int;
var randRoad10 : int;
var randRoad11 : int;
var randRoad12 : int;
var randRoad13 : int;
var randRoad14 : int;
var randRoad15 : int;
var randRoad16 : int;
var randRoad17 : int;
var randRoad18 : int;
var randRoad19 : int;
var randRoad20 : int;
function Start()
{
randRoad1 = Random.Range(1,14);
randRoad2 = Random.Range(1,14);
randRoad3 = Random.Range(1,14);
randRoad4 = Random.Range(1,14);
randRoad5 = Random.Range(1,14);
randRoad6 = Random.Range(1,14);
randRoad7 = Random.Range(1,14);
randRoad8 = Random.Range(1,14);
randRoad9 = Random.Range(1,14);
randRoad10 = Random.Range(1,14);
randRoad11 = Random.Range(1,14);
randRoad12 = Random.Range(1,14);
randRoad13 = Random.Range(1,14);
randRoad14 = Random.Range(1,14);
randRoad15 = Random.Range(1,14);
randRoad16 = Random.Range(1,14);
randRoad17 = Random.Range(1,14);
randRoad18 = Random.Range(1,14);
randRoad19 = Random.Range(1,14);
randRoad20 = Random.Range(1,14);
print(randRoad1);
//road1
if(randRoad1 == 1)
{
Instantiate(road1,Vector3(0,1,0), Quaternion.Euler(0,0,0));
}
else if(randRoad1 == 2)
{
Instantiate(road2,Vector3(0,1,0), Quaternion.Euler(0,0,0));
}
else if(randRoad1 == 3)
{
Instantiate(road3,Vector3(0,1,0), Quaternion.Euler(0,0,0));
}
else if(randRoad1 == 4)
{
Instantiate(road4,Vector3(0,1,0), Quaternion.Euler(0,0,0));
}
else if(randRoad1 == 5)
{
Instantiate(road5,Vector3(0,1,0), Quaternion.Euler(0,0,0));
}
else if(randRoad1 == 6)
{
Instantiate(road6,Vector3(0,1,0), Quaternion.Euler(0,0,0));
}
else if(randRoad1 == 7)
{
Instantiate(road7,Vector3(0,1,0), Quaternion.Euler(0,0,0));
}
else if(randRoad1 == 8)
{
Instantiate(road8,Vector3(0,1,0), Quaternion.Euler(0,0,0));
}
else if(randRoad1 == 9)
{
Instantiate(road9,Vector3(0,1,0), Quaternion.Euler(0,0,0));
}
else if(randRoad1 == 10)
{
Instantiate(road10,Vector3(0,1,0), Quaternion.Euler(0,0,0));
}
else if(randRoad1 == 11)
{
Instantiate(road11,Vector3(0,1,0), Quaternion.Euler(0,0,0));
}
else if(randRoad1 == 12)
{
Instantiate(road12,Vector3(0,1,0), Quaternion.Euler(0,0,0));
}
else if(randRoad1 == 13)
{
Instantiate(road13,Vector3(0,1,0), Quaternion.Euler(0,0,0));
}
else if(randRoad1 == 14)
{
Instantiate(road14,Vector3(0,1,0), Quaternion.Euler(0,0,0));
}
//road2
if(randRoad2 == 1)
{
Instantiate(road1,Vector3(0,1,10), Quaternion.Euler(0,0,0));
}
if(randRoad2 == 2)
{
Instantiate(road2,Vector3(0,1,10), Quaternion.Euler(0,0,0));
}
if(randRoad2 == 3)
{
Instantiate(road3,Vector3(0,1,10), Quaternion.Euler(0,0,0));
}
if(randRoad2 == 4)
{
Instantiate(road4,Vector3(0,1,10), Quaternion.Euler(0,0,0));
}
if(randRoad2 == 5)
{
Instantiate(road5,Vector3(0,1,10), Quaternion.Euler(0,0,0));
}
if(randRoad2 == 6)
{
Instantiate(road6,Vector3(0,1,10), Quaternion.Euler(0,0,0));
}
if(randRoad2 == 7)
{
Instantiate(road7,Vector3(0,1,10), Quaternion.Euler(0,0,0));
}
if(randRoad2 == 8)
{
Instantiate(road8,Vector3(0,1,10), Quaternion.Euler(0,0,0));
}
if(randRoad2 == 9)
{
Instantiate(road9,Vector3(0,1,10), Quaternion.Euler(0,0,0));
}
if(randRoad2 == 10)
{
Instantiate(road10,Vector3(0,1,10), Quaternion.Euler(0,0,0));
}
if(randRoad2 == 11)
{
Instantiate(road11,Vector3(0,1,10), Quaternion.Euler(0,0,0));
}
if(randRoad2 == 12)
{
Instantiate(road12,Vector3(0,1,10), Quaternion.Euler(0,0,0));
}
if(randRoad2 == 13)
{
Instantiate(road13,Vector3(0,1,10), Quaternion.Euler(0,0,0));
}
if(randRoad2 == 14)
{
Instantiate(road14,Vector3(0,1,10), Quaternion.Euler(0,0,0));
}
//road 3
if(randRoad3 == 1)
{
Instantiate(road1,Vector3(0,1,20), Quaternion.Euler(0,0,0));
}
if(randRoad3 == 2)
{
Instantiate(road2,Vector3(0,1,20), Quaternion.Euler(0,0,0));
}
if(randRoad3 == 3)
{
Instantiate(road3,Vector3(0,1,20), Quaternion.Euler(0,0,0));
}
if(randRoad3 == 4)
{
Instantiate(road4,Vector3(0,1,20), Quaternion.Euler(0,0,0));
}
if(randRoad3 == 5)
{
Instantiate(road5,Vector3(0,1,20), Quaternion.Euler(0,0,0));
}
if(randRoad3 == 6)
{
Instantiate(road6,Vector3(0,1,20), Quaternion.Euler(0,0,0));
}
if(randRoad3 == 7)
{
Instantiate(road7,Vector3(0,1,20), Quaternion.Euler(0,0,0));
}
if(randRoad3 == 8)
{
Instantiate(road8,Vector3(0,1,20), Quaternion.Euler(0,0,0));
}
if(randRoad3 == 9)
{
Instantiate(road9,Vector3(0,1,20), Quaternion.Euler(0,0,0));
}
if(randRoad3 == 10)
{
Instantiate(road10,Vector3(0,1,20), Quaternion.Euler(0,0,0));
}
if(randRoad3 == 11)
{
Instantiate(road11,Vector3(0,1,20), Quaternion.Euler(0,0,0));
}
if(randRoad3 == 12)
{
Instantiate(road12,Vector3(0,1,20), Quaternion.Euler(0,0,0));
}
if(randRoad3 == 13)
{
Instantiate(road13,Vector3(0,1,20), Quaternion.Euler(0,0,0));
}
if(randRoad3 == 14)
{
Instantiate(road14,Vector3(0,1,20), Quaternion.Euler(0,0,0));
}
//road 4
if(randRoad4 == 1)
{
Instantiate(road1,Vector3(0,1,30), Quaternion.Euler(0,0,0));
}
if(randRoad4 == 2)
{
Instantiate(road2,Vector3(0,1,30), Quaternion.Euler(0,0,0));
}
if(randRoad4 == 3)
{
Instantiate(road3,Vector3(0,1,30), Quaternion.Euler(0,0,0));
}
if(randRoad4 == 4)
{
Instantiate(road4,Vector3(0,1,30), Quaternion.Euler(0,0,0));
}
if(randRoad4 == 5)
{
Instantiate(road5,Vector3(0,1,30), Quaternion.Euler(0,0,0));
}
if(randRoad4 == 6)
{
Instantiate(road6,Vector3(0,1,30), Quaternion.Euler(0,0,0));
}
if(randRoad4 == 7)
{
Instantiate(road7,Vector3(0,1,30), Quaternion.Euler(0,0,0));
}
if(randRoad4 == 8)
{
Instantiate(road8,Vector3(0,1,30), Quaternion.Euler(0,0,0));
}
if(randRoad4 == 9)
{
Instantiate(road9,Vector3(0,1,30), Quaternion.Euler(0,0,0));
}
if(randRoad4 == 10)
{
Instantiate(road10,Vector3(0,1,30), Quaternion.Euler(0,0,0));
}
if(randRoad4 == 11)
{
Instantiate(road11,Vector3(0,1,30), Quaternion.Euler(0,0,0));
}
if(randRoad4 == 12)
{
Instantiate(road12,Vector3(0,1,30), Quaternion.Euler(0,0,0));
}
if(randRoad4 == 13)
{
Instantiate(road13,Vector3(0,1,30), Quaternion.Euler(0,0,0));
}
if(randRoad4 == 14)
{
Instantiate(road14,Vector3(0,1,30), Quaternion.Euler(0,0,0));
}
//road 5
if(randRoad5 == 1)
{
Instantiate(road1,Vector3(0,1,40), Quaternion.Euler(0,0,0));
}
if(randRoad5 == 2)
{
Instantiate(road2,Vector3(0,1,40), Quaternion.Euler(0,0,0));
}
if(randRoad5 == 3)
{
Instantiate(road3,Vector3(0,1,40), Quaternion.Euler(0,0,0));
}
if(randRoad5 == 4)
{
Instantiate(road4,Vector3(0,1,40), Quaternion.Euler(0,0,0));
}
if(randRoad5 == 5)
{
Instantiate(road5,Vector3(0,1,40), Quaternion.Euler(0,0,0));
}
if(randRoad5 == 6)
{
Instantiate(road6,Vector3(0,1,40), Quaternion.Euler(0,0,0));
}
if(randRoad5 == 7)
{
Instantiate(road7,Vector3(0,1,40), Quaternion.Euler(0,0,0));
}
if(randRoad5 == 8)
{
Instantiate(road8,Vector3(0,1,40), Quaternion.Euler(0,0,0));
}
if(randRoad5 == 9)
{
Instantiate(road9,Vector3(0,1,40), Quaternion.Euler(0,0,0));
}
if(randRoad5 == 10)
{
Instantiate(road10,Vector3(0,1,40), Quaternion.Euler(0,0,0));
}
if(randRoad5 == 11)
{
Instantiate(road11,Vector3(0,1,40), Quaternion.Euler(0,0,0));
}
if(randRoad5 == 12)
{
Instantiate(road12,Vector3(0,1,40), Quaternion.Euler(0,0,0));
}
if(randRoad5 == 13)
{
Instantiate(road13,Vector3(0,1,40), Quaternion.Euler(0,0,0));
}
if(randRoad5 == 14)
{
Instantiate(road14,Vector3(0,1,40), Quaternion.Euler(0,0,0));
}
//road 6
if(randRoad6 == 1)
{
Instantiate(road1,Vector3(0,1,50), Quaternion.Euler(0,0,0));
}
if(randRoad6 == 2)
{
Instantiate(road2,Vector3(0,1,50), Quaternion.Euler(0,0,0));
}
if(randRoad6 == 3)
{
Instantiate(road3,Vector3(0,1,50), Quaternion.Euler(0,0,0));
}
if(randRoad6 == 4)
{
Instantiate(road4,Vector3(0,1,50), Quaternion.Euler(0,0,0));
}
if(randRoad6 == 5)
{
Instantiate(road5,Vector3(0,1,50), Quaternion.Euler(0,0,0));
}
if(randRoad6 == 6)
{
Instantiate(road6,Vector3(0,1,50), Quaternion.Euler(0,0,0));
}
if(randRoad6 == 7)
{
Instantiate(road7,Vector3(0,1,50), Quaternion.Euler(0,0,0));
}
if(randRoad6 == 8)
{
Instantiate(road8,Vector3(0,1,50), Quaternion.Euler(0,0,0));
}
if(randRoad6 == 9)
{
Instantiate(road9,Vector3(0,1,50), Quaternion.Euler(0,0,0));
}
if(randRoad6 == 10)
{
Instantiate(road10,Vector3(0,1,50), Quaternion.Euler(0,0,0));
}
if(randRoad6 == 11)
{
Instantiate(road11,Vector3(0,1,50), Quaternion.Euler(0,0,0));
}
if(randRoad6 == 12)
{
Instantiate(road12,Vector3(0,1,50), Quaternion.Euler(0,0,0));
}
if(randRoad6 == 13)
{
Instantiate(road13,Vector3(0,1,50), Quaternion.Euler(0,0,0));
}
if(randRoad6 == 14)
{
Instantiate(road14,Vector3(0,1,50), Quaternion.Euler(0,0,0));
}
//road7
if(randRoad7 == 1)
{
Instantiate(road1,Vector3(0,1,60), Quaternion.Euler(0,0,0));
}
if(randRoad7 == 2)
{
Instantiate(road2,Vector3(0,1,60), Quaternion.Euler(0,0,0));
}
if(randRoad7 == 3)
{
Instantiate(road3,Vector3(0,1,60), Quaternion.Euler(0,0,0));
}
if(randRoad7 == 4)
{
Instantiate(road4,Vector3(0,1,60), Quaternion.Euler(0,0,0));
}
if(randRoad7 == 5)
{
Instantiate(road5,Vector3(0,1,60), Quaternion.Euler(0,0,0));
}
if(randRoad7 == 6)
{
Instantiate(road6,Vector3(0,1,60), Quaternion.Euler(0,0,0));
}
if(randRoad7 == 7)
{
Instantiate(road7,Vector3(0,1,60), Quaternion.Euler(0,0,0));
}
if(randRoad7 == 8)
{
Instantiate(road8,Vector3(0,1,60), Quaternion.Euler(0,0,0));
}
if(randRoad7 == 9)
{
Instantiate(road9,Vector3(0,1,60), Quaternion.Euler(0,0,0));
}
if(randRoad7 == 10)
{
Instantiate(road10,Vector3(0,1,60), Quaternion.Euler(0,0,0));
}
if(randRoad7 == 11)
{
Instantiate(road11,Vector3(0,1,60), Quaternion.Euler(0,0,0));
}
if(randRoad7 == 12)
{
Instantiate(road12,Vector3(0,1,60), Quaternion.Euler(0,0,0));
}
if(randRoad7 == 13)
{
Instantiate(road13,Vector3(0,1,60), Quaternion.Euler(0,0,0));
}
if(randRoad7 == 14)
{
Instantiate(road14,Vector3(0,1,60), Quaternion.Euler(0,0,0));
}
//road 8
if(randRoad8 == 1)
{
Instantiate(road1,Vector3(0,1,70), Quaternion.Euler(0,0,0));
}
if(randRoad8 == 2)
{
Instantiate(road2,Vector3(0,1,70), Quaternion.Euler(0,0,0));
}
if(randRoad8 == 3)
{
Instantiate(road3,Vector3(0,1,70), Quaternion.Euler(0,0,0));
}
if(randRoad8 == 4)
{
Instantiate(road4,Vector3(0,1,70), Quaternion.Euler(0,0,0));
}
if(randRoad8 == 5)
{
Instantiate(road5,Vector3(0,1,70), Quaternion.Euler(0,0,0));
}
if(randRoad8 == 6)
{
Instantiate(road6,Vector3(0,1,70), Quaternion.Euler(0,0,0));
}
if(randRoad8 == 7)
{
Instantiate(road7,Vector3(0,1,70), Quaternion.Euler(0,0,0));
}
if(randRoad8 == 8)
{
Instantiate(road8,Vector3(0,1,70), Quaternion.Euler(0,0,0));
}
if(randRoad8 == 9)
{
Instantiate(road9,Vector3(0,1,70), Quaternion.Euler(0,0,0));
}
if(randRoad8 == 10)
{
Instantiate(road10,Vector3(0,1,70), Quaternion.Euler(0,0,0));
}
if(randRoad8 == 11)
{
Instantiate(road11,Vector3(0,1,70), Quaternion.Euler(0,0,0));
}
if(randRoad8 == 12)
{
Instantiate(road12,Vector3(0,1,70), Quaternion.Euler(0,0,0));
}
if(randRoad8 == 13)
{
Instantiate(road13,Vector3(0,1,70), Quaternion.Euler(0,0,0));
}
if(randRoad8 == 14)
{
Instantiate(road14,Vector3(0,1,70), Quaternion.Euler(0,0,0));
}
//road 9
if(randRoad9 == 1)
{
Instantiate(road1,Vector3(0,1,80), Quaternion.Euler(0,0,0));
}
if(randRoad9 == 2)
{
Instantiate(road2,Vector3(0,1,80), Quaternion.Euler(0,0,0));
}
if(randRoad9 == 3)
{
Instantiate(road3,Vector3(0,1,80), Quaternion.Euler(0,0,0));
}
if(randRoad9 == 4)
{
Instantiate(road4,Vector3(0,1,80), Quaternion.Euler(0,0,0));
}
if(randRoad9 == 5)
{
Instantiate(road5,Vector3(0,1,80), Quaternion.Euler(0,0,0));
}
if(randRoad9 == 6)
{
Instantiate(road6,Vector3(0,1,80), Quaternion.Euler(0,0,0));
}
if(randRoad9 == 7)
{
Instantiate(road7,Vector3(0,1,80), Quaternion.Euler(0,0,0));
}
if(randRoad9 == 8)
{
Instantiate(road8,Vector3(0,1,80), Quaternion.Euler(0,0,0));
}
if(randRoad9 == 9)
{
Instantiate(road9,Vector3(0,1,80), Quaternion.Euler(0,0,0));
}
if(randRoad9 == 10)
{
Instantiate(road10,Vector3(0,1,80), Quaternion.Euler(0,0,0));
}
if(randRoad9 == 11)
{
Instantiate(road11,Vector3(0,1,80), Quaternion.Euler(0,0,0));
}
if(randRoad9 == 12)
{
Instantiate(road12,Vector3(0,1,80), Quaternion.Euler(0,0,0));
}
if(randRoad9 == 13)
{
Instantiate(road13,Vector3(0,1,80), Quaternion.Euler(0,0,0));
}
if(randRoad9 == 14)
{
Instantiate(road14,Vector3(0,1,80), Quaternion.Euler(0,0,0));
}
//road 10
if(randRoad10 == 1)
{
Instantiate(road1,Vector3(0,1,90), Quaternion.Euler(0,0,0));
}
if(randRoad10 == 2)
{
Instantiate(road2,Vector3(0,1,90), Quaternion.Euler(0,0,0));
}
if(randRoad10 == 3)
{
Instantiate(road3,Vector3(0,1,90), Quaternion.Euler(0,0,0));
}
if(randRoad10 == 4)
{
Instantiate(road4,Vector3(0,1,90), Quaternion.Euler(0,0,0));
}
if(randRoad10 == 5)
{
Instantiate(road5,Vector3(0,1,90), Quaternion.Euler(0,0,0));
}
if(randRoad10 == 6)
{
Instantiate(road6,Vector3(0,1,90), Quaternion.Euler(0,0,0));
}
if(randRoad10 == 7)
{
Instantiate(road7,Vector3(0,1,90), Quaternion.Euler(0,0,0));
}
if(randRoad10 == 8)
{
Instantiate(road8,Vector3(0,1,90), Quaternion.Euler(0,0,0));
}
if(randRoad10 == 9)
{
Instantiate(road9,Vector3(0,1,90), Quaternion.Euler(0,0,0));
}
if(randRoad10 == 10)
{
Instantiate(road10,Vector3(0,1,90), Quaternion.Euler(0,0,0));
}
if(randRoad10 == 11)
{
Instantiate(road11,Vector3(0,1,90), Quaternion.Euler(0,0,0));
}
if(randRoad10 == 12)
{
Instantiate(road12,Vector3(0,1,90), Quaternion.Euler(0,0,0));
}
if(randRoad10 == 13)
{
Instantiate(road13,Vector3(0,1,90), Quaternion.Euler(0,0,0));
}
if(randRoad10 == 14)
{
Instantiate(road14,Vector3(0,1,90), Quaternion.Euler(0,0,0));
}
//road 11
if(randRoad11 == 1)
{
Instantiate(road1,Vector3(0,1,100), Quaternion.Euler(0,0,0));
}
if(randRoad11 == 2)
{
Instantiate(road2,Vector3(0,1,100), Quaternion.Euler(0,0,0));
}
if(randRoad11 == 3)
{
Instantiate(road3,Vector3(0,1,100), Quaternion.Euler(0,0,0));
}
if(randRoad11 == 4)
{
Instantiate(road4,Vector3(0,1,100), Quaternion.Euler(0,0,0));
}
if(randRoad11 == 5)
{
Instantiate(road5,Vector3(0,1,100), Quaternion.Euler(0,0,0));
}
if(randRoad11 == 6)
{
Instantiate(road6,Vector3(0,1,100), Quaternion.Euler(0,0,0));
}
if(randRoad11 == 7)
{
Instantiate(road7,Vector3(0,1,100), Quaternion.Euler(0,0,0));
}
if(randRoad11 == 8)
{
Instantiate(road8,Vector3(0,1,100), Quaternion.Euler(0,0,0));
}
if(randRoad11 == 9)
{
Instantiate(road9,Vector3(0,1,100), Quaternion.Euler(0,0,0));
}
if(randRoad11 == 10)
{
Instantiate(road10,Vector3(0,1,100), Quaternion.Euler(0,0,0));
}
if(randRoad11 == 11)
{
Instantiate(road11,Vector3(0,1,100), Quaternion.Euler(0,0,0));
}
if(randRoad11 == 12)
{
Instantiate(road12,Vector3(0,1,100), Quaternion.Euler(0,0,0));
}
if(randRoad11 == 13)
{
Instantiate(road13,Vector3(0,1,100), Quaternion.Euler(0,0,0));
}
if(randRoad11 == 14)
{
Instantiate(road14,Vector3(0,1,100), Quaternion.Euler(0,0,0));
}
//road 12
if(randRoad12 == 1)
{
Instantiate(road1,Vector3(0,1,110), Quaternion.Euler(0,0,0));
}
if(randRoad12 == 2)
{
Instantiate(road2,Vector3(0,1,110), Quaternion.Euler(0,0,0));
}
if(randRoad12 == 3)
{
Instantiate(road3,Vector3(0,1,110), Quaternion.Euler(0,0,0));
}
if(randRoad12 == 4)
{
Instantiate(road4,Vector3(0,1,110), Quaternion.Euler(0,0,0));
}
if(randRoad12 == 5)
{
Instantiate(road5,Vector3(0,1,110), Quaternion.Euler(0,0,0));
}
if(randRoad12 == 6)
{
Instantiate(road6,Vector3(0,1,110), Quaternion.Euler(0,0,0));
}
if(randRoad12 == 7)
{
Instantiate(road7,Vector3(0,1,110), Quaternion.Euler(0,0,0));
}
if(randRoad12 == 8)
{
Instantiate(road8,Vector3(0,1,110), Quaternion.Euler(0,0,0));
}
if(randRoad12 == 10)
{
Instantiate(road10,Vector3(0,1,110), Quaternion.Euler(0,0,0));
}
if(randRoad12 == 11)
{
Instantiate(road11,Vector3(0,1,110), Quaternion.Euler(0,0,0));
}
if(randRoad12 == 12)
{
Instantiate(road12,Vector3(0,1,110), Quaternion.Euler(0,0,0));
}
if(randRoad12 == 13)
{
Instantiate(road13,Vector3(0,1,110), Quaternion.Euler(0,0,0));
}
if(randRoad12 == 14)
{
Instantiate(road14,Vector3(0,1,110), Quaternion.Euler(0,0,0));
}
//road 13
if(randRoad13 == 1)
{
Instantiate(road1,Vector3(0,1,120), Quaternion.Euler(0,0,0));
}
if(randRoad13 == 2)
{
Instantiate(road2,Vector3(0,1,120), Quaternion.Euler(0,0,0));
}
if(randRoad13 == 3)
{
Instantiate(road3,Vector3(0,1,120), Quaternion.Euler(0,0,0));
}
if(randRoad13 == 4)
{
Instantiate(road4,Vector3(0,1,120), Quaternion.Euler(0,0,0));
}
if(randRoad13 == 5)
{
Instantiate(road5,Vector3(0,1,120), Quaternion.Euler(0,0,0));
}
if(randRoad13 == 6)
{
Instantiate(road6,Vector3(0,1,120), Quaternion.Euler(0,0,0));
}
if(randRoad13 == 7)
{
Instantiate(road7,Vector3(0,1,120), Quaternion.Euler(0,0,0));
}
if(randRoad13 == 8)
{
Instantiate(road8,Vector3(0,1,120), Quaternion.Euler(0,0,0));
}
if(randRoad13 == 9)
{
Instantiate(road9,Vector3(0,1,120), Quaternion.Euler(0,0,0));
}
if(randRoad13 == 10)
{
Instantiate(road10,Vector3(0,1,120), Quaternion.Euler(0,0,0));
}
if(randRoad13 == 11)
{
Instantiate(road11,Vector3(0,1,120), Quaternion.Euler(0,0,0));
}
if(randRoad13 == 12)
{
Instantiate(road12,Vector3(0,1,120), Quaternion.Euler(0,0,0));
}
if(randRoad13 == 13)
{
Instantiate(road13,Vector3(0,1,120), Quaternion.Euler(0,0,0));
}
if(randRoad13 == 14)
{
Instantiate(road14,Vector3(0,1,120), Quaternion.Euler(0,0,0));
}
//road 14
if(randRoad14 == 1)
{
Instantiate(road1,Vector3(0,1,130), Quaternion.Euler(0,0,0));
}
if(randRoad14 == 2)
{
Instantiate(road2,Vector3(0,1,130), Quaternion.Euler(0,0,0));
}
if(randRoad14 == 3)
{
Instantiate(road3,Vector3(0,1,130), Quaternion.Euler(0,0,0));
}
if(randRoad14 == 4)
{
Instantiate(road4,Vector3(0,1,130), Quaternion.Euler(0,0,0));
}
if(randRoad14 == 5)
{
Instantiate(road5,Vector3(0,1,130), Quaternion.Euler(0,0,0));
}
if(randRoad14 == 6)
{
Instantiate(road6,Vector3(0,1,130), Quaternion.Euler(0,0,0));
}
if(randRoad14 == 7)
{
Instantiate(road7,Vector3(0,1,130), Quaternion.Euler(0,0,0));
}
if(randRoad14 == 8)
{
Instantiate(road8,Vector3(0,1,130), Quaternion.Euler(0,0,0));
}
if(randRoad14 == 9)
{
Instantiate(road9,Vector3(0,1,130), Quaternion.Euler(0,0,0));
}
if(randRoad14 == 10)
{
Instantiate(road10,Vector3(0,1,130), Quaternion.Euler(0,0,0));
}
if(randRoad14 == 11)
{
Instantiate(road11,Vector3(0,1,130), Quaternion.Euler(0,0,0));
}
if(randRoad14 == 12)
{
Instantiate(road12,Vector3(0,1,130), Quaternion.Euler(0,0,0));
}
if(randRoad14 == 13)
{
Instantiate(road13,Vector3(0,1,130), Quaternion.Euler(0,0,0));
}
if(randRoad14 == 14)
{
Instantiate(road14,Vector3(0,1,130), Quaternion.Euler(0,0,0));
}
//road 15
if(randRoad15 == 1)
{
Instantiate(road1,Vector3(0,1,140), Quaternion.Euler(0,0,0));
}
if(randRoad15 == 2)
{
Instantiate(road2,Vector3(0,1,140), Quaternion.Euler(0,0,0));
}
if(randRoad15 == 3)
{
Instantiate(road3,Vector3(0,1,140), Quaternion.Euler(0,0,0));
}
if(randRoad15 == 4)
{
Instantiate(road4,Vector3(0,1,140), Quaternion.Euler(0,0,0));
}
if(randRoad15 == 5)
{
Instantiate(road5,Vector3(0,1,140), Quaternion.Euler(0,0,0));
}
if(randRoad15 == 6)
{
Instantiate(road6,Vector3(0,1,140), Quaternion.Euler(0,0,0));
}
if(randRoad15 == 7)
{
Instantiate(road7,Vector3(0,1,140), Quaternion.Euler(0,0,0));
}
if(randRoad15 == 8)
{
Instantiate(road8,Vector3(0,1,140), Quaternion.Euler(0,0,0));
}
if(randRoad15 == 9)
{
Instantiate(road9,Vector3(0,1,140), Quaternion.Euler(0,0,0));
}
if(randRoad15 == 10)
{
Instantiate(road10,Vector3(0,1,140), Quaternion.Euler(0,0,0));
}
if(randRoad15 == 11)
{
Instantiate(road11,Vector3(0,1,140), Quaternion.Euler(0,0,0));
}
if(randRoad15 == 12)
{
Instantiate(road12,Vector3(0,1,140), Quaternion.Euler(0,0,0));
}
if(randRoad15 == 13)
{
Instantiate(road13,Vector3(0,1,140), Quaternion.Euler(0,0,0));
}
if(randRoad15 == 14)
{
Instantiate(road14,Vector3(0,1,140), Quaternion.Euler(0,0,0));
}
//road 16
if(randRoad16 == 1)
{
Instantiate(road1,Vector3(0,1,150), Quaternion.Euler(0,0,0));
}
if(randRoad16 == 2)
{
Instantiate(road2,Vector3(0,1,150), Quaternion.Euler(0,0,0));
}
if(randRoad16 == 3)
{
Instantiate(road3,Vector3(0,1,150), Quaternion.Euler(0,0,0));
}
if(randRoad16 == 4)
{
Instantiate(road4,Vector3(0,1,150), Quaternion.Euler(0,0,0));
}
if(randRoad16 == 5)
{
Instantiate(road5,Vector3(0,1,150), Quaternion.Euler(0,0,0));
}
if(randRoad16 == 6)
{
Instantiate(road6,Vector3(0,1,150), Quaternion.Euler(0,0,0));
}
if(randRoad16 == 7)
{
Instantiate(road7,Vector3(0,1,150), Quaternion.Euler(0,0,0));
}
if(randRoad16 == 8)
{
Instantiate(road8,Vector3(0,1,150), Quaternion.Euler(0,0,0));
}
if(randRoad16 == 9)
{
Instantiate(road9,Vector3(0,1,150), Quaternion.Euler(0,0,0));
}
if(randRoad16 == 10)
{
Instantiate(road10,Vector3(0,1,150), Quaternion.Euler(0,0,0));
}
if(randRoad16 == 11)
{
Instantiate(road11,Vector3(0,1,150), Quaternion.Euler(0,0,0));
}
if(randRoad16 == 12)
{
Instantiate(road12,Vector3(0,1,150), Quaternion.Euler(0,0,0));
}
if(randRoad16 == 13)
{
Instantiate(road13,Vector3(0,1,150), Quaternion.Euler(0,0,0));
}
if(randRoad16 == 14)
{
Instantiate(road14,Vector3(0,1,150), Quaternion.Euler(0,0,0));
}
//road17
if(randRoad17 == 1)
{
Instantiate(road1,Vector3(0,1,160), Quaternion.Euler(0,0,0));
}
if(randRoad17 == 2)
{
Instantiate(road2,Vector3(0,1,160), Quaternion.Euler(0,0,0));
}
if(randRoad17 == 3)
{
Instantiate(road3,Vector3(0,1,160), Quaternion.Euler(0,0,0));
}
if(randRoad17 == 4)
{
Instantiate(road4,Vector3(0,1,160), Quaternion.Euler(0,0,0));
}
if(randRoad17 == 5)
{
Instantiate(road5,Vector3(0,1,160), Quaternion.Euler(0,0,0));
}
if(randRoad17 == 6)
{
Instantiate(road6,Vector3(0,1,160), Quaternion.Euler(0,0,0));
}
if(randRoad17 == 7)
{
Instantiate(road7,Vector3(0,1,160), Quaternion.Euler(0,0,0));
}
if(randRoad17 == 8)
{
Instantiate(road8,Vector3(0,1,160), Quaternion.Euler(0,0,0));
}
if(randRoad17 == 9)
{
Instantiate(road9,Vector3(0,1,160), Quaternion.Euler(0,0,0));
}
if(randRoad17 == 10)
{
Instantiate(road10,Vector3(0,1,160), Quaternion.Euler(0,0,0));
}
if(randRoad17 == 11)
{
Instantiate(road11,Vector3(0,1,160), Quaternion.Euler(0,0,0));
}
if(randRoad17 == 12)
{
Instantiate(road12,Vector3(0,1,160), Quaternion.Euler(0,0,0));
}
if(randRoad17 == 13)
{
Instantiate(road13,Vector3(0,1,160), Quaternion.Euler(0,0,0));
}
if(randRoad17 == 14)
{
Instantiate(road14,Vector3(0,1,160), Quaternion.Euler(0,0,0));
}
//road18
if(randRoad18 == 1)
{
Instantiate(road1,Vector3(0,1,170), Quaternion.Euler(0,0,0));
}
if(randRoad18 == 2)
{
Instantiate(road2,Vector3(0,1,170), Quaternion.Euler(0,0,0));
}
if(randRoad18 == 3)
{
Instantiate(road3,Vector3(0,1,170), Quaternion.Euler(0,0,0));
}
if(randRoad18 == 4)
{
Instantiate(road4,Vector3(0,1,170), Quaternion.Euler(0,0,0));
}
if(randRoad18 == 5)
{
Instantiate(road5,Vector3(0,1,170), Quaternion.Euler(0,0,0));
}
if(randRoad18 == 6)
{
Instantiate(road6,Vector3(0,1,170), Quaternion.Euler(0,0,0));
}
if(randRoad18 == 7)
{
Instantiate(road7,Vector3(0,1,170), Quaternion.Euler(0,0,0));
}
if(randRoad18 == 8)
{
Instantiate(road8,Vector3(0,1,170), Quaternion.Euler(0,0,0));
}
if(randRoad18 == 9)
{
Instantiate(road9,Vector3(0,1,170), Quaternion.Euler(0,0,0));
}
if(randRoad18 == 10)
{
Instantiate(road10,Vector3(0,1,170), Quaternion.Euler(0,0,0));
}
if(randRoad18 == 11)
{
Instantiate(road11,Vector3(0,1,170), Quaternion.Euler(0,0,0));
}
if(randRoad18 == 12)
{
Instantiate(road12,Vector3(0,1,170), Quaternion.Euler(0,0,0));
}
if(randRoad18 == 13)
{
Instantiate(road13,Vector3(0,1,170), Quaternion.Euler(0,0,0));
}
if(randRoad18 == 14)
{
Instantiate(road14,Vector3(0,1,170), Quaternion.Euler(0,0,0));
}
//road 19
if(randRoad19 == 1)
{
Instantiate(road1,Vector3(0,1,180), Quaternion.Euler(0,0,0));
}
if(randRoad19 == 2)
{
Instantiate(road2,Vector3(0,1,180), Quaternion.Euler(0,0,0));
}
if(randRoad19 == 3)
{
Instantiate(road3,Vector3(0,1,180), Quaternion.Euler(0,0,0));
}
if(randRoad19 == 4)
{
Instantiate(road4,Vector3(0,1,180), Quaternion.Euler(0,0,0));
}
if(randRoad19 == 5)
{
Instantiate(road5,Vector3(0,1,180), Quaternion.Euler(0,0,0));
}
if(randRoad19 == 6)
{
Instantiate(road6,Vector3(0,1,180), Quaternion.Euler(0,0,0));
}
if(randRoad19 == 7)
{
Instantiate(road7,Vector3(0,1,180), Quaternion.Euler(0,0,0));
}
if(randRoad19 == 8)
{
Instantiate(road8,Vector3(0,1,180), Quaternion.Euler(0,0,0));
}
if(randRoad19 == 9)
{
Instantiate(road9,Vector3(0,1,180), Quaternion.Euler(0,0,0));
}
if(randRoad19 == 10)
{
Instantiate(road10,Vector3(0,1,180), Quaternion.Euler(0,0,0));
}
if(randRoad19 == 11)
{
Instantiate(road11,Vector3(0,1,180), Quaternion.Euler(0,0,0));
}
if(randRoad19 == 12)
{
Instantiate(road12,Vector3(0,1,180), Quaternion.Euler(0,0,0));
}
if(randRoad19 == 13)
{
Instantiate(road13,Vector3(0,1,180), Quaternion.Euler(0,0,0));
}
if(randRoad19 == 14)
{
Instantiate(road14,Vector3(0,1,180), Quaternion.Euler(0,0,0));
}
//road 20
if(randRoad20 == 1)
{
Instantiate(road1,Vector3(0,1,190), Quaternion.Euler(0,0,0));
}
if(randRoad20 == 2)
{
Instantiate(road2,Vector3(0,1,190), Quaternion.Euler(0,0,0));
}
if(randRoad20 == 3)
{
Instantiate(road3,Vector3(0,1,190), Quaternion.Euler(0,0,0));
}
if(randRoad20 == 4)
{
Instantiate(road4,Vector3(0,1,190), Quaternion.Euler(0,0,0));
}
if(randRoad20 == 5)
{
Instantiate(road5,Vector3(0,1,190), Quaternion.Euler(0,0,0));
}
if(randRoad20 == 6)
{
Instantiate(road6,Vector3(0,1,190), Quaternion.Euler(0,0,0));
}
if(randRoad20 == 7)
{
Instantiate(road7,Vector3(0,1,190), Quaternion.Euler(0,0,0));
}
if(randRoad20 == 8)
{
Instantiate(road8,Vector3(0,1,190), Quaternion.Euler(0,0,0));
}
if(randRoad20 == 9)
{
Instantiate(road9,Vector3(0,1,190), Quaternion.Euler(0,0,0));
}
if(randRoad20 == 10)
{
Instantiate(road10,Vector3(0,1,190), Quaternion.Euler(0,0,0));
}
if(randRoad20 == 11)
{
Instantiate(road11,Vector3(0,1,190), Quaternion.Euler(0,0,0));
}
if(randRoad20 == 12)
{
Instantiate(road1,Vector3(0,1,190), Quaternion.Euler(0,0,0));
}
if(randRoad20 == 13)
{
Instantiate(road1,Vector3(0,1,190), Quaternion.Euler(0,0,0));
}
if(randRoad20 == 14)
{
Instantiate(road1,Vector3(0,1,190), Quaternion.Euler(0,0,0));
}
}
FYI condition 14 in Random.Range won't fire as the upper bound of the INT version of Random.Range is Exclusive and will not ever be picked. To get a condition 14 your upper bound needs to be 15. This does not apply to the FLOAT version of Random.Range.
In my opinion, you should replace all these if statements with a switch, or at least, something more efficient.
Also, as your random calls are in start() they will only produce numbers once; is that what you intended?
Dude...arrays. Never, ever have fifty million separate variables and if/thens all over the place, that's just impossible to work with.
I'm a noob btw guys lol. And yeah, it only generates that tacks once every time it starts.
Even if you're a "noob", look up some basic tutorials, please. Even when I had just started out, I never tried to make any code like this - it's just unmanageable, and if you'd tried to learn a little before jumping straight in, you'd know that.
Answer by Bunny83 · Oct 13, 2013 at 09:27 PM
O M G
First of all your whole script could be reduced to:
var roadPrefabs : GameObject[];
function Start()
{
for (var z = 0; z < 200; z += 10)
{
Instantiate(roadPrefabs[Random.Range(0, roadPrefabs.Length)], Vector3(0,1,z), Quaternion.identity);
}
}
This even allows you to add as much road parts as you like.
I don't really get what's your actual problem. What do you mean by 2 numbers? You mean you get 2 print-logs in your console? In this case you probably have attached your script to multiple objects. Make sure it's only attached to one object.
To find out to which objects the script is attached to add this:
// ...
function Start()
{
Debug.Log("Attached to " + gameObject.name);
// ...
If you also get this line two times it's attached to 2 objects.
Bunny, easiest way to find a script is to right click it in editor and click Find all references :)
roadPrefabs.Length + 1 !!!
Only if you want to get an out of bounds error.
@meat5000 Arrays are 0-indexed, meaning the last index of the array is the length - 1, so Bunny83 was perfectly correct.
Yes, as I said, arrays are 0 indexed. Length
gives you the number of elements in the array, so if there is an array with just one element, Length
will be 1, but the element will be accessed with array[0]
, and array[1]
will throw an error. Indices of an array span from 0 to Length-1. The code given already relies on the upper bound being exclusive.
Ok cool. Thanks for that.
@shatley123, I don't know how relevent this is to you but I just noticed you haven't changed some numbers I assume you'd meant to
if(randRoad20 == 12)
{
Instantiate(road1,Vector3(0,1,190), Quaternion.Euler(0,0,0));
}
if(randRoad20 == 13)
{
Instantiate(road1,Vector3(0,1,190), Quaternion.Euler(0,0,0));
}
if(randRoad20 == 14)
{
Instantiate(road1,Vector3(0,1,190), Quaternion.Euler(0,0,0));
}
All instantiate road1. Is that intentional?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How can I Instantiate and do it in a random spot in a random spot? 2 Answers
Random number on game start for a variable 1 Answer
Help with spawning an object in a random location 1 Answer
Random.Range Vector3 2 Answers