- Home /
Random.Range statement question
I'm trying to select a random number from 1 - 4. Here's my script - what's wrong with it? It only gives numbers from 1-3.
var ground : GameObject; var numberOfBuildings : int = 20; var numberOfRows : int = 5; var building1 : GameObject; var building2 : GameObject; var building3 : GameObject; var building4 : GameObject; var position = Vector3 (10,0,10); var buildingType = 4; var zchange = 10; var nothing : GameObject;
function Start (){
var newObj = Instantiate (ground, Vector3(0,-0.1,0), transform.rotation); newObj.transform.localScale.z = numberOfBuildings*20; newObj.transform.localScale.x = numberOfRows*40;
}
function Update () { if (numberOfBuildings > 0) {
buildingType = Random.Range (1,4);
print (buildingType);
switch (buildingType) { case (1) : Instantiate (building1, position, transform.rotation); position += Vector3 (0,0,zchange); numberOfBuildings--; break;
case (2) : Instantiate (building2, position, transform.rotation); position += Vector3 (0,0,zchange); numberOfBuildings--; break;
case (3) : Instantiate (building3, position, transform.rotation); position += Vector3 (0,0,zchange); numberOfBuildings--; break;
case (4) : Instantiate (building4, position, transform.rotation); position += Vector3 (0,0,zchange); numberOfBuildings--; break;
} } }
Thanks!
When you get this finished I'd like to see it $$anonymous$$uzz. It looks interesting.
Answer by Justin Warner · Apr 24, 2011 at 08:28 PM
static function Range (min : int, max : int) : int Description
Returns a random integer number between min [inclusive] and max [exclusive] (Read Only).
If max equals min, min will be returned. The returned value will never be max unless min equals max.
Make it:
Random.Range(1,5);
http://unity3d.com/support/documentation/ScriptReference/Random.Range.html
Exclusive means it doesn't read that one...
Think of in Java like doing a substring function... If you know that...
But yea, this'll work =).
Yea... I think it's funny that if you use floats ins$$anonymous$$d of an int, it is inclusive inclusive... So then the second number IS included... Haha.
Also, when I used (1,3), it gave me those 3 values. I will never get scripting.
Hmm, that's very strange... It might be a small glitch, never know...