- Home /
JS Loop question
I'm still trying to get this sorted...this is my first ever experience with editor scripting, and I'm grateful to everyone who's helped. Only question is : why isn't my for loop working?
class CityWizard extends ScriptableWizard {
var ground : GameObject; var numberOfBuildings : int = 20; var inititalBuildingNumber : int =20; var numberOfRows : int = 5; var building1 : GameObject; var building2 : GameObject; var building3 : GameObject; var MyPosition = Vector3(10, 0, 10); var buildingType = 4; var zchange = 10; var nothing : GameObject; var rowchange = 0; var rowchangeIncrease = 10;
@MenuItem("GameObject/City!")
static function City (){ ScriptableWizard.DisplayWizard("Title", CityWizard, "City", ""); }
function OnWizardCreate() {
inititalBuildingNumber = numberOfBuildings;
var newObj = Instantiate (ground, Vector3(0,-0.1,0), Quaternion.identity);
newObj.transform.localScale.z = numberOfBuildings*40;
newObj.transform.localScale.x = numberOfRows*100;
for(numberOfBuildings=20; numberOfBuildings <=0; numberOfBuildings--){
buildingType = Random.Range (1,5);
print (buildingType);
switch (buildingType) {
case (1) :
Instantiate (building1, MyPosition, Quaternion.identity); MyPosition += Vector3 (0,0,zchange);
break;
case (2) : Instantiate (building2, MyPosition, Quaternion.identity); MyPosition += Vector3 (0,0,zchange + 3);
break;
case (3) : Instantiate (building3, MyPosition, Quaternion.identity); MyPosition += Vector3 (0,0,zchange+3);
break;
case (4) : Instantiate (nothing, MyPosition, Quaternion.identity); MyPosition += Vector3 (0,0,zchange);
break;
}
}
}
function OnWizardUpdate (){ helpString = ("Please assign a prefab to create!");
} }
Answer by Joshua · Apr 30, 2011 at 04:18 PM
It's because
for(numberOfBuildings=20; numberOfBuildings <=0; numberOfBuildings--)
This means start out at 20, increment by minus one as long as it's smaller then zero. It start's at 20 so it's not smaller then zero so it'll not even run once. I think it's a typo of >= 0 or you want something else but don't understand for loops.
G'd luck!
Answer by Kourosh · Apr 30, 2011 at 03:46 PM
Random.Range returns a float value. You need to have an integer instead in order to use switch case. Change that line to this:
buildinType = Mathf.Floor(Random.Range(1,6));
Doesn't help, sorry. You misspelt buildingType, but even with correct spelling it doesn't work. It doesn't even print the building type.
This may have been right as well, so I'll give you the upvote.
a=Random.Range(b,c); doesn't return a float. It returns either a float or an int depending on weather a, b and c are all ints. If they are it returns an int between b [inclusive] and c [exlusive]. Else it's a float between b [inclusive] and c [inclusive]. Be very aware of the difference between the two.
Also, his suggestion is just wrong since it will return the values 1,2,3,4 and 5 where you just want 1 to 4. You were using it correctly initially. Because you declare buildingType at the top as 4 and not 4.0 the compiler knows it's an int.
I mean no offence by the way, $$anonymous$$ourosh! $$anonymous$$udo's for trying to help - just trying to counter the widespread misunderstanding about Random.Range :)