I am trying to generate a simple map like this. Everything works as I want, but I am new to programing. Is it a good way to do something like this? And if not, could someone explain to me where and why it is bad to do something like this? And how bad is it on performance?
public GameObject cube;
public GameObject cube1;
public GameObject cube2;
Vector3 here;
void Start () {
for (int y = 0; y < 50; y++) {
for (int x = 0; x < 10; x++) {
here = new Vector3(x,y,0);
int change = (int)Random.Range(0,19);
switch (change){
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 10:
Instantiate(cube,here,Quaternion.identity);
break;
case 11: case 12: case 13: case 14: case 15: case 16:
Instantiate(cube1,here,Quaternion.identity);
break;
case 17: case 18:
Instantiate(cube2,here,Quaternion.identity);
break;
}
}
}
Running this code generates something random like this:
Instantiate
takes 3 things (GameObject
you want to make, coordinates where to make it, and rotation), and it makes a copy of GameObject
- prefab in those coordinates.
Cube
- RedCube1
- GreenCube2
- Blue
The reason for all of this is that I want to learn to make something random likea map area of the game.
Instantiate
method do? What is the end result of this? Some more context about what kind of game we're dealing with here would make it a lot easier for us to come up with better answers. \$\endgroup\$ – Simon Forsberg Mar 18 '15 at 23:07GameObject
code and the code for theInstantiate
method as well? (don't worry about them being too long) \$\endgroup\$ – Simon Forsberg Mar 18 '15 at 23:22