armedunity.com uses cookies to improve user experience.

By continuing to use this site, you agree to allow us to store cookies on your computer.

• 0

# Place Buildings on Terrain

## Question

hey i wrote this code to place Buildings on my Terrain but the code seems to take for ever or ends up with an array index error. i hope you can help me

-the variable size is just terraindata.alphamapheight

-Terrains[] is an Array with all Terrains in the scene in it (so Terrains is just a simple Terrain)

-TerrainPosX is the global x position of the terrain in worldspace

-TerrainPosZ is the same for Z

Got really stuck on this one :/

Thanks for your help ... if you do need more context please let me know and ill be happy to provide it... i just wanted to keep the snipped short n simple (still hope that im just too dumb to spot something obvious)

Edited by Erarnitox

## Recommended Posts

• 2

Make sure that heightmap is the size of the terrain (heightMap[terrainWidth, terrainHeight]). In addition, just to make the error isn't going to happen to mess it up on the sides, when you say heightMap[x,y] = posy; enclose it in an if statement saying if(x >= 0 && x < terrainWidth && y >= 0 && y < terrainHeight) that way if you place near the edge of the terrain it won't throw the error as well

• 1

##### Share on other sites

• 1

Your for(int y.. loop is saying x < instead of y <. Could be the culprit of how long it takes as well as your error

• 1

##### Share on other sites
• 1

Shouldn't those be a + 3 not a - 3? Because your loop is using + 3 but your check is using - 3

• 1

##### Share on other sites
• 0

thank you  i was literally watching this code for about 1h and didnt see this lol

edit: still got the same problem tho.

Edited by Erarnitox

##### Share on other sites
• 0

it should already be that size but ill double check that later.

and for the edges i have this if statement: (which should do the same job (posx-terrainPox should be posxterrainPosx-1 if you want to be exact))

• 0