I’m trying to create a terrain out of a grid of individual 4-vertex planes which will eventually be customized for different materials(i.e. clay, dirt, sand, etc). I am able to create a grid of flat planes, but when I try to alter the height of individual vertices, only the bottom left square(Tile00) renders and the remaining are instantiated as empties.
Here is my script. I can’t quite figure out what’s wrong, and I’m not getting any errors other than two variables not being used.
using UnityEngine;
using System.Collections;
public class SpawnTester : MonoBehaviour {
public GameObject board;
public Transform tile_prefab_;
public int board_size_x_;
public int board_size_z_;
public int nameX;
public int nameZ;
void Start(){
GenerateTiles();
GenerateHeight();
}
void GenerateTiles () {
board = new GameObject();
board.name = "Board";
for ( int x = 0; x < board_size_x_; x++ ) {
for ( int z = 0; z < board_size_z_; z++ ) {
Transform tile = (Transform)Instantiate(tile_prefab_,new Vector3(x * 10,0,z * 10),Quaternion.identity);
tile.name = "Tile" + x + z;
tile.parent = board.transform;
}
}
}
void GenerateHeight () {
for ( int x = 0; x < board_size_x_; x++ ) {
for ( int z = 0; z < board_size_z_; z++ ) {
Transform tile = GameObject.Find("Tile" + x + z).transform;
Mesh mesh = tile.GetComponent<MeshFilter>().mesh;
Vector3[] vertices = mesh.vertices;
if(tile.name == "Tile00"){
for(int i = 0; i < vertices.Length; i++){
vertices _+= Vector3.up * Random.Range(0, 11);_
-
}*
-
mesh.vertices = vertices;*
-
mesh.RecalculateBounds();*
-
mesh.RecalculateNormals();*
-
MeshCollider meshc = tile.gameObject.AddComponent(typeof(MeshCollider)) as MeshCollider;*
-
meshc.sharedMesh = mesh;*
-
}*
-
else{*
-
int tempX = x - 1;*
-
int tempZ = z - 1;*
-
if(tempX < 0)*
-
tempX = 0;*
-
if(tempZ < 0)*
-
tempZ = 0;*
-
string leftName = "Tile" + tempX + z;*
-
string botName = "Tile" + x + tempZ;*
-
Transform leftTile = GameObject.Find(leftName).transform;*
-
Transform botTile = GameObject.Find(botName).transform;*
-
if(leftTile){*
-
Vector3[] leftVerts = leftTile.GetComponent<MeshFilter>().mesh.vertices;*
-
Vector3 leftVertTop = leftVerts[0];*
-
Vector3 leftVertBot = leftVerts[3];*
-
vertices[1] = leftVertTop;*
-
vertices[2] = leftVertTop;*
-
}*
-
else{*
_ vertices[1] += Vector3.up * Random.Range(0, 11);_
_ vertices[2] += Vector3.up * Random.Range(0, 11);_
-
}*
-
if(botTile){*
-
Vector3[] botVerts = botTile.GetComponent<MeshFilter>().mesh.vertices;*
-
Vector3 botVertLeft = botVerts[2];*
-
Vector3 botVertRight = botVerts[0];*
-
vertices[3] = botVertRight;*
-
}*
-
else{*
_ vertices[3] += Vector3.up * Random.Range(0, 11);_
-
}*
_ vertices[0] += Vector3.up * Random.Range(0, 11);_
-
mesh.vertices = vertices;*
-
mesh.RecalculateBounds();*
-
MeshCollider meshc = tile.gameObject.AddComponent(typeof(MeshCollider)) as MeshCollider;*
-
meshc.sharedMesh = mesh;*
-
}*
-
}*
-
}*
- }*
}