# If statement not working

I've been writing a code, and I can't get my if statement to work at all. here is the code - what is wrong with it? The if statment not working is the bottom one.

``````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 position = Vector3 (10,0,10);
var buildingType = 4;
var zchange = 10;
var nothing : GameObject;
var rowchange = 0;
var rowchangeIncrease = 10;

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,5);

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 (nothing, position, transform.rotation);
position += Vector3 (0,0,zchange);
numberOfBuildings--;
break;

if (numberOfBuildings == 0 && numberOfRows > 0){
print ("next");
position == Vector3 (10,0,0);
numberOfBuildings += inititalBuildingNumber;
rowchange += rowchangeIncrease;
numberOfRows--;
}
}

}
}

``````

Thanks.

You have nested it in your switch statement. Just move it outside the next closing bracket.

``````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;

function Start (){
inititalBuildingNumber = numberOfBuildings;
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,5);
print (buildingType);

switch (buildingType) {
case (1) :
Instantiate (building1, MyPosition, transform.rotation);
MyPosition += Vector3 (0,0,zchange);
numberOfBuildings--;
break;

case (2) :
Instantiate (building2, MyPosition, transform.rotation);
MyPosition += Vector3 (0,0,zchange);
numberOfBuildings--;
break;

case (3) :
Instantiate (building3, MyPosition, transform.rotation);
MyPosition += Vector3 (0,0,zchange);
numberOfBuildings--;
break;

case (4) :
Instantiate (nothing, MyPosition, transform.rotation);
MyPosition += Vector3 (0,0,zchange);
numberOfBuildings--;
break;
}
}
else {
if (numberOfRows > 0){
print ("next");
MyPosition += Vector3 (rowchangeIncrease,0,-(zchange*inititalBuildingNumber));
numberOfBuildings += inititalBuildingNumber;
rowchange += rowchangeIncrease;
numberOfRows--;
}
}
}

``````

I got it to work for me after I fiddled around for a bit and changed and reorganized some of your code

Hi,

Obviously, because it is inside the 'switch(buildingType)' so if it goes always to any case the 'switch' statement is over because of the 'break'

It should be something like this

``````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 (nothing, position, transform.rotation);
position += Vector3 (0,0,zchange);
numberOfBuildings--;
break;
}//end switch

if (numberOfBuildings == 0 && numberOfRows == 0){
print ("next");
position == Vector3 (10,0,0);
numberOfBuildings += inititalBuildingNumber;
rowchange += rowchangeIncrease;
numberOfRows--;
}

``````