- Home /
The question is answered, right answer was accepted
swich case not working properly
Hi guys, sorry for the long-ish code but i just decided to copy the entire method. In my for loop, as long as (i < worldSize), a world object will be instantiated, where index 0 is a sea tile and index 1 is a land tile. In other words this creates a small ocean world with an island. The problem i am facing is in my else if code, my switch case instantiates a large island when it should only instantiate a 1 tile island. This island should only be instantiated when (i = islandPosition), whereby island position is a random # in my worldSize.
void createWorld(){
int i;
for(i = 0; i < worldSize; i++){
linkPosition = GameObject.FindWithTag("seaLink").transform;
// removeTag();
if(i != islandPosition){
//instantiate sea
Instantiate(worldObjects[0],linkPosition.position,Quaternion.identity);
linkPosition.position += new Vector3(perfectTileLink,0,1);
}
else if (i == islandPosition){
//float islandSize = Random.Range(1,4);
int islandSize = 1;
switch (islandSize){
case 1:
Instantiate(worldObjects[1],linkPosition.position,Quaternion.identity);
linkPosition.position += new Vector3(perfectTileLink,0,1);
Debug.Log("island 1 tile");
break;
case 2:
Instantiate(worldObjects[1],linkPosition.position,Quaternion.identity);
linkPosition.position += new Vector3(perfectTileLink,0,1);
Instantiate(worldObjects[1],linkPosition.position,Quaternion.identity);
linkPosition.position += new Vector3(perfectTileLink,0,1);
Debug.Log("island 2 tile");
break;
case 3:
Instantiate(worldObjects[1],linkPosition.position,Quaternion.identity);
linkPosition.position += new Vector3(perfectTileLink,0,1);
Instantiate(worldObjects[1],linkPosition.position,Quaternion.identity);
linkPosition.position += new Vector3(perfectTileLink,0,1);
Instantiate(worldObjects[1],linkPosition.position,Quaternion.identity);
linkPosition.position += new Vector3(perfectTileLink,0,1);
Debug.Log("island 3 tile");
break;
case 4:
Instantiate(worldObjects[1],linkPosition.position,Quaternion.identity);
linkPosition.position += new Vector3(perfectTileLink,0,1);
Instantiate(worldObjects[1],linkPosition.position,Quaternion.identity);
linkPosition.position += new Vector3(perfectTileLink,0,1);
Instantiate(worldObjects[1],linkPosition.position,Quaternion.identity);
linkPosition.position += new Vector3(perfectTileLink,0,1);
Instantiate(worldObjects[1],linkPosition.position,Quaternion.identity);
linkPosition.position += new Vector3(perfectTileLink,0,1);
Debug.Log("island 4 tile");
break;
default:
Instantiate(worldObjects[0],linkPosition.position,Quaternion.identity);
linkPosition.position += new Vector3(perfectTileLink,0,1);
Debug.Log("island default tile");
break;
}
Instantiate(worldObjects[1],linkPosition.position,Quaternion.identity);
linkPosition.position += new Vector3(perfectTileLink,0,1);
Instantiate(worldObjects[1],linkPosition.position,Quaternion.identity);
linkPosition.position += new Vector3(perfectTileLink,0,1);
}
}
Instantiate(worldObjects[2],linkPosition.position += new Vector3(-20,8,0),Quaternion.identity);
//Instantiating portal;
int fishingSpots = Random.Range(0,3);
for(int f = 0; f <= fishingSpots; f++){
waterTiles = GameObject.FindGameObjectsWithTag("sea");
// Debug.Log("Number of waterTiles = "+waterTiles.Length);
int chosenTile = Random.Range(0,waterTiles.Length);
Instantiate(seaMiniGame[0],new Vector3(waterTiles[chosenTile].transform.position.x,-4,0),Quaternion.identity);
for(int number = 0; number <= smallIslands; number++ ){
Instantiate(smallIsland[Random.Range(0,smallIsland.Length)],
waterTiles[Random.Range(0,waterTiles.Length)].transform.position,Quaternion.identity);
}
}
}
realized my error. I had another block of code which instantiated an island in each loop just outside my case statement
If your question was answered or solved, mark the question as solved.