- Home /
Huge C# Procedural Generation Errors. Why?
Hello, I've been using a tutorial I found to create a voxel system for the roguelike I'm making. I just really don't know where I went wrong since I wrote the identical code. Should the script have been cut into multiple scripts or what?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PolygonGenerator : MonoBehaviour {
//This first list contains every vertex of the mesh that we are going to render.
public List<Vector3> newVertices = new List<Vector3>();
//The triangles tell Unity how to build each section of the mesh joining the vertices.
public List<int> newTriangles = new List<int>();
//The UV list is unimportant right now but it tells Unity how the texture is aligned
//on each polygon.
public List<Vector2> newUV = new List<Vector2>();
public List<Vector3> colVertices = new List<Vector3> ();
public List<int> colTriangles = new List<int>();
private int colCount;
private MeshCollider col;
//A mesh is made up of the vertices, triangles and UVs we are going to define,
//after we make them up we'll save them as this mesh.
private Mesh mesh;
private float tUnit = 0.25f;
private Vector2 tStone = new Vector2 (0, 0);
private Vector2 tGrass = new Vector2 (0, 1);
private int squareCount;
// Use this for initialization
void Start () {
mesh = GetComponent<MeshFilter> ().mesh;
float x = transform.position.x;
float y = transform.position.y;
float z = transform.position.z;
GenTerrain ();
BuildMesh ();
UpdateMesh ();
col = GetComponent<MeshCollider> ();
}
void GenSquare(int x, int y, Vector2 texture){
newVertices.Add(new Vector3 (x, y, z));
newVertices.Add(new Vector3 (x + 1, y, z));
newVertices.Add (new Vector3 (x + 1, y - 1, z));
newVertices.Add (new Vector3 (x, y - 1, z));
newTriangles.Add (squareCount*4);
newTriangles.Add ((squareCount*4)+1);
newTriangles.Add ((squareCount*4)+3);
newTriangles.Add ((squareCount*4)+1);
newTriangles.Add ((squareCount*4)+2);
newTriangles.Add ((squareCount*4)+3);
newUV.Add (new Vector2 (tUnit * texture.x, tUnit * texture.y + tUnit));
newUV.Add (new Vector2 (tUnit * texture.x + tUnit, tUnit * texture.y + tUnit));
newUV.Add (new Vector2 (tUnit * texture.x + tUnit, tUnit * texture.y));
newUV.Add (new Vector2 (tUnit * texture.x, tUnit * texture.y));
squareCount++;
}
// Update is called once per frame
void Update () {
mesh.Clear ();
mesh.vertices = newVertices.ToArray ();
mesh.triangles = newTriangles.ToArray ();
mesh.uv = newUV.ToArray (); //Post-Initial Script Addition
mesh.Optimize ();
mesh.RecalculateNormals ();
}
void UpdateMesh () {
mesh.Clear ();
mesh.vertices = newVertices.ToArray ();
mesh.triangles = newTriangles.ToArray ();
mesh.uv = newUV.ToArray (); //Post-Initial Script Addition
mesh.Optimize ();
mesh.RecalculateNormals ();
squareCount = 0;
newVertices.Clear ();
newTriangles.Clear ();
newUV.Clear ();
Mesh newMesh = new Mesh ();
newMesh.vertices = colVertices.ToArray ();
newMesh.triangles = colTriangles.ToArray ();
col.sharedMesh = newMesh;
colVertices.Clear ();
colTriangles.Clear ();
colCount = 0;
}
void GenCollider(int x, int y,){
//Top
colVertices.Add (new Vector3 (x , y , 1));
colVertices.Add (new Vector3 (x + 1 , y , 1));
colVertices.Add (new Vector3 (x + 1 , y , 0));
colVertices.Add (new Vector3 (x , y , 0));
ColliderTriangles ();
colCount++;
//Bot
colVertices.Add (new Vector3 (x , y -1 , 0));
colVertices.Add (new Vector3 (x + 1 , y -1 , 0));
colVertices.Add (new Vector3 (x + 1 , y -1 , 1 ));
colVertices.Add (new Vector3 (x , y -1 , 1 ));
ColliderTriangles ();
colCount++;
//Left
colVertices.Add (new Vector3 (x , y -1 , 1));
colVertices.Add (new Vector3 (x , y , 1));
colVertices.Add (new Vector3 (x , y , 0 ));
colVertices.Add (new Vector3 (x , y -1 , 0));
ColliderTriangles();
colCount++;
//Right
colVertices.Add (new Vector3 (x +1 , y , 1));
colVertices.Add (new Vector3 (x +1 , y -1 , 1));
colVertices.Add (new Vector3 (x +1 , y -1 , 0));
colVertices.Add (new Vector3 (x +1 , y , 0));
ColliderTriangles ();
colCount++;
}
void ColliderTriangles(){
colTriangles.Add (colCount*4);
colTriangles.Add ((colCount*4)+1);
colTriangles.Add ((colCount*4)+3);
colTriangles.Add ((colCount*4)+1);
colTriangles.Add ((colCount*4)+2);
colTriangles.Add ((colCount*4)+3);
colCount++;
}
public byte[,] blocks;
void GenTerrain(){
blocks=new byte[10,10];
for(int px=0; px<blocks.GetLength(0); px++) {
for(int py=0;py<blocks.GetLength(1);py++){
if(py==5){
blocks[px,py]=2;
} else if(py<5){
blocks[px,py]=1;
}
}
}
}
void BuildMesh(){
for (int px=0; px<blocks.GetLength(0); px++) {
for(int py=0;py<blocks.GetLength (1);py++){
//If the block is not air.
if(blocks[px,py]);
//GenCollider here, this will apply it.
//To every block other than air.
GenCollider (px,py);
if(blocks[px,py]==1){
GenSquare (px,py,tStone);
else if(blocks[px,py]==2{
GenSquare(px,py,tGrass);
}
}//End air block check.
}
}
}
}
Answer by robertbu · May 03, 2014 at 06:43 AM
There are problems...likely related to your transcription of the code. For example on line 109:
void GenCollider(int x, int y,){
...the comma to the right of the 'y' should not be there. Fixing this problem reveals other issues. On line 193, you are missing the closing ')' for the 'if' statement. In addition there is some sort of bracketing trouble in this area. That is, an else much be associated with a fully bracketed ({}) 'if' statement, and there is no closing bracket on line 192. I suggest you reexamine your transcription in this area.
I reviewed the code and did two things and now I'm down to a single parsing error on line 200, which is closing the entire script. What cold it be?
Typically a parsing error is caused a missing '}' at the end of the file. If you cannot figure it out, edit your question above and insert your latest code. I'll take a look tomorrow morning.