- Home /
How to set the layer to a programatically generated terrain?
Hi,
I have a runtima generated terrain like this:
public enum Layer
{
Terrain = 1 << 8,
Tower = 1 << 9,
Enemy = 1 << 10,
TowerBeingSet = 1 << 11,
PathEnd = 1 << 12
};
this.terrainData = new TerrainData ();
// terrainData init //
AssetDatabase.CreateAsset (terrainData, "Assets/New Terrain.asset");
this.terrainGameObject = Terrain.CreateTerrainGameObject (this.terrainData);
And then I try to set up the layer for it with:
this.terrainGameObject.layer = (int)Layer.Terrain;
foreach (Transform trans in terrainGameObject.GetComponentsInChildren<Transform>(true)) {
trans.gameObject.layer = (int)Layer.Terrain;
}
But when I run the scene the terrain go has its layer set to "Default" and all events that depend on the layer obviously doesn't work.
So, what would be the right way to set a terrains layer?
Thx in advance.
Imagine you want to use Layer#07... Then replace
(int) Layer.Terrain
with
1 << 7
Hi, thx for the answer but as u can see on the edited post, the Layer.Terrain is actually what u re proposing.
oh, right... then give me a second to search from my sources... I should have done something like this.
Hum... that sounds me strange, but on the source code that i wrote some time ago, I didn't shift the terrain flag... And it worked, please try without shifting and tell me. :P
Well... using
Layer$$anonymous$$ask.NameToLayer ("Terrain");
Seems to work... odd... I use that enum for a lot of other things and it works fine :S
Answer by TieSKey · Jan 06, 2013 at 04:09 PM
Using
LayerMask.NameToLayer ("Terrain");
instead of the shifted ints enum works fine.
Answer by benito_nimi · Apr 09, 2015 at 07:49 PM
terrainGameObject.gameObject.layer = LayerMask.NameToLayer("LAYER_NAME");