c# - Having some issues with procedurally generating a map -


i'm building simple 2d tile map. spawns few different grass tiles in random locations here's looks like:

 public int tileheight; //y   public int tilewidth; //x   int tileheightcounter;  int tilewidthcounter;   public gameobject[] floortiles;    void start(){      tileheightcounter = 0;       tilewidthcounter = 0;        while(tileheightcounter < tileheight){           vector3 tilespawnpoint = new vector3 (tilewidthcounter, tileheightcounter, 0);            gameobject groundtiles = (gameobject)instantiate (floortiles[random.range(0, floortiles.length)], tilespawnpoint, quaternion.identity);           groundtiles.transform.parent = transform;            if (tilewidthcounter == tilewidth) {              tilewidthcounter = 0;              tileheightcounter++;          }           tilewidthcounter++;      }  } 

i've run 2 problems- tileheight 10 , tilewidth 10 map generates should 10x10 100 total tiles randomly distributed.

instead 2 weird things occurring first if map 10x10 generates 10x9 meaning stops 1 layer short on y axis. second grid(grass tile) being created @ 0,0 rest of map being created @ least x being 1 meaning map has single tile attached bottom left sticking out.

i'm bit confused whats going on here or how fix it. appreciated.

the issue you're using < less than, once hits tileheight exits loop 1 iteration early. make <=.


Comments