- Home /
Error in code! Help me please! =(
Hello everyone, I'm new to c # and I can not understand the error code:
using UnityEngine; using System.Collections; using System.Collections.Generic;
public enum TerrainType { Lowlands, Highlands, Mountains }
[RequireComponent (typeof(MeshFilter))] [RequireComponent (typeof(MeshCollider))] public class Chunk : MonoBehaviour {
public int height = 40;
public int groundHeight = 8;
public byte[,,] map;
protected Mesh mesh;
protected List<Vector3> verts = new List<Vector3>();
protected List<int> tris = new List<int>();
protected List<Vector2> uv = new List<Vector2>();
protected MeshCollider meshCollider;
public List<GameObject> targets = new List<GameObject>();
protected bool initialized = false;
protected int width;
// Use this for initialization
void Start () {
Terrain terrain = Camera.main.GetComponent<Terrain>();
width = terrain.chunkSize;
map = new byte[width, height, width];
float baseTerrainHeight = GetGroundHeight(GetTerrainFor(transform.position.x, transform.position.z));
float[,] terrainHeights = new float[3,3];
for (int dx = -1; dx <= 1; dx++)
{
for (int dz = -1; dz <= 1; dz++)
{
float myX = transform.position.x + dx * width;
float myZ = transform.position.z + dz * width;
terrainHeights[dx + 1, dz + 1] = GetGroundHeight(GetTerrainFor(myX, myZ)) + baseTerrainHeight / 2;
}
}
for (int x = 0; x < width; x++)
{
float xPercent = (float)x / (float)width;
float xHeight1 = CurvePoint(xPercent, terrainHeights[0,0],terrainHeights[1,0],terrainHeights[2,0]);
float xHeight2 = CurvePoint(xPercent, terrainHeights[0,1],terrainHeights[1,1],terrainHeights[2,1]);
float xHeight3 = CurvePoint(xPercent, terrainHeights[0,2],terrainHeights[1,2],terrainHeights[2,2]);
for (int z = 0; z < width; z++)
{
float zPercent = (float)z / (float)width;
float tileHeight = CurvePoint(xHeight1, xHeight2, xHeight3);
for (int y = 0; y < tileHeight; y++);
map[x, y, z] = 1;
}
}
}
/*
for (int x = 0; x < width; x++)
{
for (int y = 0; y < groundHeight; y++)
{
for (int z = 0; z < width; z++)
{
map[x, y, z] = 1;
}
}
}
//*/
for (int dx = -1; dx <= 1; dx++)
{
for (int dz = -1; dz <= 1; dz++)
{
float myX = transform.position.x + dx * width;
float myZ = transform.position.z + dz * width;
List<LandBrush> brushes = GetBrushesFor(myX, myZ);
}
for (int a = 0; a < brushes.Count; a++)
{
brushes[a].ApplyBrush(this);
}
}
} initialized = true; Regenerate();
}
// Update is called once per frame
void Update () {
}
public void CloseMeshTarget() {
mesh.vertices = verts.ToArray();
mesh.triangles = tris.ToArray();
mesh.uv = uv.ToArray();
mesh.RecalculateNormals();
meshCollider.sharedMesh = mesh;
}
public void CreateMeshTarget(bool reset=false) {
meshCollider = GetComponent<MeshCollider>();
mesh = new Mesh();
GetComponent<MeshFilter>().mesh = mesh;
verts.Clear();
tris.Clear();
uv.Clear();
}
public void DrawBrick(int x, int y, int z, byte block) {
Vector3 start = new Vector3(x, y, z);
Vector3 offset1, offset2;
if (IsTransparent(x, y - 1, z))
{
offset1 = Vector3.left;
offset2 = Vector3.back;
DrawFace(start + Vector3.right, offset1, offset2, block);
}
if (IsTransparent(x, y + 1, z))
{
offset1 = Vector3.right;
offset2 = Vector3.back;
DrawFace(start + Vector3.up, offset1, offset2, block);
}
if (IsTransparent(x - 1, y , z))
{
offset1 = Vector3.up;
offset2 = Vector3.back;
DrawFace(start , offset1, offset2, block);
}
if (IsTransparent(x + 1, y, z ))
{
offset1 = Vector3.down;
offset2 = Vector3.back;
DrawFace(start + Vector3.right + Vector3.up, offset1, offset2, block);
}
if (IsTransparent(x, y, z - 1))
{
offset1 = Vector3.left;
offset2 = Vector3.up;
DrawFace(start + Vector3.right + Vector3.back, offset1, offset2, block);
}
if (IsTransparent(x, y, z + 1))
{
offset1 = Vector3.right;
offset2 = Vector3.up;
DrawFace(start, offset1, offset2, block);
}
}
public void DrawFace(Vector3 start, Vector3 offset1, Vector3 offset2, byte block)
{
int index = verts.Count;
verts.Add (start);
verts.Add (start + offset1);
verts.Add (start + offset2);
verts.Add (start + offset1 + offset2);
Vector2 uvBase;
switch (block)
{
default:
uvBase = new Vector2(0.25f,0.25f);
break;
case 2:
uvBase = new Vector2(0.75f,0.75f);
break;
case 3:
uvBase = new Vector2(0.25f,0.75f);
break;
case 4:
uvBase = new Vector2(0.75f,0.25f);
break;
}
if ((offset1 == Vector3.right) && (offset2 == Vector3.back))
{
uv.Add (uvBase);
uv.Add (uvBase + new Vector2(0.125f, 0));
uv.Add (uvBase + new Vector2(0, 0.125f));
uv.Add (uvBase + new Vector2(0.125f, 0.125f));
}
else
{
uv.Add (uvBase);
uv.Add (uvBase + new Vector2(0.125f, 0));
uv.Add (uvBase + new Vector2(0, 0.125f));
uv.Add (uvBase + new Vector2(0.125f, 0.125f));
}
tris.Add(index + 0);
tris.Add(index + 1);
tris.Add(index + 2);
tris.Add(index + 3);
tris.Add(index + 2);
tris.Add(index + 1);
}
public bool IsTransparent(int x, int y, int z)
{
if ((x< 0) || (y < 0) || (z < 0) || (x >= width) || (y >= height) || (z >= width)) return true;
return map[x, y, z] == 0;
}
public void Regenerate() {
if (! initialized) return;
CreateMeshTarget(true);
mesh.triangles = tris.ToArray();
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
for (int z = 0; z < width; z++)
{
byte block = map[x,y,z];
if (block == 0) continue;
DrawBrick(x, y, z, block);
}
}
}
CloseMeshTarget();
}
public void SetBrick(int x, int y, int z, byte block)
{
if (y == 0) return;
x -= Mathf.RoundToInt(transform.position.x);
y -= Mathf.RoundToInt(transform.position.y);
z -= Mathf.RoundToInt(transform.position.z);
if ((x < 0) || (y < 0) || (z < 0) || (x >= width) || (y >= height) || (z >= width)) return;
if (map[x, y, z] != block)
{
map[x, y, z] = block;
Regenerate();
}
}
public static List<LandBrush> GetBrushesFor(float x, float z) {
List<LandBrush> brushes = new List<LandBrush>();
TerrainType terrainType = GetTerrainFor(x,z);
Terrain terrain = Camera.main.GetComponent<Terrain>();
Random.seed = terrain.seed + Mathf.FloorToInt(x * 7 + z * 13);
float numBrushes = 12;
while (numBrushes > 0)
{
numBrushes--;
brushes.Add (new LandBrush(x, z, terrain.chunkSize, terrainType));
}
return brushes;
}
public static TerrainType GetTerrainFor(float x, float z) {
Terrain terrain = Camera.main.GetComponent<Terrain>();
Random.seed = terrain.seed + Mathf.FloorToInt(x * 7 + z * 13);
return (TerrainType)Mathf.FloorToInt(Random.value * 3);
}
public static float GetGroundHeight(TerrainType terrainType){
switch (terrainType)
{
case TerrainType.Lowlands:
return 8;
case TerrainType.Highlands:
return 12;
case TerrainType.Mountains:
return 30;
default:
return 1;
}
}
public static float CurvePoint(float percent, float val1, float val2, val3) {
float p1 = (1 - percent) * val1 + percent * val2;
float p2 = (1 - percent) * val2 + percent * val3;
return (1 - percent) * p1 + percent * p2;
}
}
Displays the error:
Assets/Scripts/Chunk.cs(98,19): error CS8025: Parsing error Assets/Scripts/Chunk.cs(92,47): error CS1519: Unexpected symbol ++' in class, struct, or interface member declaration Assets/Scripts/Chunk.cs(92,39): error CS1519: Unexpected symbol
<=' in class, struct, or interface member declaration Assets/Scripts/Chunk.cs(90,39): error CS1519: Unexpected symbol ++' in class, struct, or interface member declaration Assets/Scripts/Chunk.cs(90,31): error CS1519: Unexpected symbol
<=' in class, struct, or interface member declaration Assets/Scripts/Chunk.cs(90,11): error CS1519: Unexpected symbol `for' in class, struct, or interface member declaration
HELP please!
This is not a simple syntax error fix. The Start() function is ter$$anonymous$$ated on line 60. That means all the code between that point and the start of the Update() function is outside of any function and just floating in the middle of the class.