I made these 2 scripts and when I finished them they both got the same errors and I don’t know what they are from.
and here is the scripts.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Chunk : MonoBehaviour {
public GameObject worldGO;
private World world;
private List<Vector3> newVertices = new List<Vector3>();
private List<int> newTriangles = new List<int>();
private List<Vector2> newUV = new List<Vector2>();
private float tUnit = 0.25f;
private Vector2 tStone = new Vector2 (1, 0);
private Vector2 tGrass = new Vector2 (0, 1);
private Vector2 tGrassTop = new Vector2 (1, 1);
private Mesh mesh;
private MeshCollider col;
private int faceCount;
public int chunkSize=16;
public int chunkX;
public int chunkY;
public int chunkZ;
// Use this for initialization
void Start () {
world=worldGO.GetComponent(\"World\") as World;
mesh = GetComponent<MeshFilter> ().mesh;
col = GetComponent<MeshCollider> ();
GenerateMesh();
}
// Update is called once per frame
void Update () {
}
void GenerateMesh(){
for (int x=0; x<chunkSize; x++){
for (int y=0; y<chunkSize; y++){
for (int z=0; z<chunkSize; z++){
//This code will run for every block in the chunk
if(Block(x,y,z)!=0){
if(Block(x,y+1,z)==0){
//Block above is air
CubeTop(x,y,z,Block(x,y,z));
}
if(Block(x,y-1,z)==0){
//Block below is air
CubeBot(x,y,z,Block(x,y,z));
}
if(Block(x+1,y,z)==0){
//Block east is air
CubeEast(x,y,z,Block(x,y,z));
}
if(Block(x-1,y,z)==0){
//Block west is air
CubeWest(x,y,z,Block(x,y,z));
}
if(Block(x,y,z+1)==0){
//Block north is air
CubeNorth(x,y,z,Block(x,y,z));
}
if(Block(x,y,z-1)==0){
//Block south is air
CubeSouth(x,y,z,Block(x,y,z));
}
}
}
}
}
UpdateMesh ();
}
byte Block(int x, int y, int z){
return world.Block(x+chunkX,y+chunkY,z+chunkZ);
}
void CubeTop (int x, int y, int z, byte block) {
newVertices.Add(new Vector3 (x, y, z + 1));
newVertices.Add(new Vector3 (x + 1, y, z + 1));
newVertices.Add(new Vector3 (x + 1, y, z ));
newVertices.Add(new Vector3 (x, y, z ));
Vector2 texturePos=new Vector2(0,0);
if(Block(x,y,z)==1){
texturePos=tStone;
} else if(Block(x,y,z)==2){
texturePos=tGrassTop;
}
Cube (texturePos);
}
void CubeNorth (int x, int y, int z, byte block) {
newVertices.Add(new Vector3 (x + 1, y-1, z + 1));
newVertices.Add(new Vector3 (x + 1, y, z + 1));
newVertices.Add(new Vector3 (x, y, z + 1));
newVertices.Add(new Vector3 (x, y-1, z + 1));
Vector2 texturePos=new Vector2(0,0);
if(Block(x,y,z)==1){
texturePos=tStone;
} else if(Block(x,y,z)==2){
texturePos=tGrass;
}
Cube (texturePos);
}
void CubeEast (int x, int y, int z, byte block) {
newVertices.Add(new Vector3 (x + 1, y - 1, z));
newVertices.Add(new Vector3 (x + 1, y, z));
newVertices.Add(new Vector3 (x + 1, y, z + 1));
newVertices.Add(new Vector3 (x + 1, y - 1, z + 1));
Vector2 texturePos=new Vector2(0,0);
if(Block(x,y,z)==1){
texturePos=tStone;
} else if(Block(x,y,z)==2){
texturePos=tGrass;
}
Cube (texturePos);
}
void CubeSouth (int x, int y, int z, byte block) {
newVertices.Add(new Vector3 (x, y - 1, z));
newVertices.Add(new Vector3 (x, y, z));
newVertices.Add(new Vector3 (x + 1, y, z));
newVertices.Add(new Vector3 (x + 1, y - 1, z));
Vector2 texturePos=new Vector2(0,0);
if(Block(x,y,z)==1){
texturePos=tStone;
} else if(Block(x,y,z)==2){
texturePos=tGrass;
}
Cube (texturePos);
}
void CubeWest (int x, int y, int z, byte block) {
newVertices.Add(new Vector3 (x, y- 1, z + 1));
newVertices.Add(new Vector3 (x, y, z + 1));
newVertices.Add(new Vector3 (x, y, z));
newVertices.Add(new Vector3 (x, y - 1, z));
Vector2 texturePos=new Vector2(0,0);
if(Block(x,y,z)==1){
texturePos=tStone;
} else if(Block(x,y,z)==2){
texturePos=tGrass;
}
Cube (texturePos);
}
void CubeBot (int x, int y, int z, byte block) {
newVertices.Add(new Vector3 (x, y-1, z ));
newVertices.Add(new Vector3 (x + 1, y-1, z ));
newVertices.Add(new Vector3 (x + 1, y-1, z + 1));
newVertices.Add(new Vector3 (x, y-1, z + 1));
Vector2 texturePos=new Vector2(0,0);
if(Block(x,y,z)==1){
texturePos=tStone;
} else if(Block(x,y,z)==2){
texturePos=tGrass;
}
Cube (texturePos);
}
void Cube (Vector2 texturePos) {
newTriangles.Add(faceCount * 4 ); //1
newTriangles.Add(faceCount * 4 + 1 ); //2
newTriangles.Add(faceCount * 4 + 2 ); //3
newTriangles.Add(faceCount * 4 ); //1
newTriangles.Add(faceCount * 4 + 2 ); //3
newTriangles.Add(faceCount * 4 + 3 ); //4
newUV.Add(new Vector2 (tUnit * texturePos.x + tUnit, tUnit * texturePos.y));
newUV.Add(new Vector2 (tUnit * texturePos.x + tUnit, tUnit * texturePos.y + tUnit));
newUV.Add(new Vector2 (tUnit * texturePos.x, tUnit * texturePos.y + tUnit));
newUV.Add(new Vector2 (tUnit * texturePos.x, tUnit * texturePos.y));
faceCount++; // Add this line
}
void UpdateMesh ()
{
mesh.Clear ();
mesh.vertices = newVertices.ToArray();
mesh.uv = newUV.ToArray();
mesh.triangles = newTriangles.ToArray();
mesh.Optimize ();
mesh.RecalculateNormals ();
col.sharedMesh=null;
col.sharedMesh=mesh;
newVertices.Clear();
newUV.Clear();
newTriangles.Clear();
faceCount=0;
}
}
and script 2
using UnityEngine;
using System.Collections;
public class World : MonoBehaviour {
public GameObject chunk;
public GameObject[,,] chunks;
public int chunkSize=16;
public byte[,,] data;
public int worldX=16;
public int worldY=16;
public int worldZ=16;
// Use this for initialization
void Start () {
data = new byte[worldX,worldY,worldZ];
for (int x=0; x<worldX; x++){
for (int z=0; z<worldZ; z++){
int stone=PerlinNoise(x,0,z,10,3,1.2f);
stone+= PerlinNoise(x,300,z,20,4,0)+10;
int dirt=PerlinNoise(x,100,z,50,3,0)+1;
for (int y=0; y<worldY; y++){
if(y<=stone){
data[x,y,z]=1;
} else if(y<=dirt+stone){
data[x,y,z]=2;
}
}
}
}
chunks=new GameObject[Mathf.FloorToInt(worldX/chunkSize),Mathf.FloorToInt(worldY/chunkSize),Mathf.FloorToInt(worldZ/chunkSize)];
for (int x=0; x<chunks.GetLength(0); x++){
for (int y=0; y<chunks.GetLength(1); y++){
for (int z=0; z<chunks.GetLength(2); z++){
chunks[x,y,z]= Instantiate(chunk,new Vector3(x*chunkSize,y*chunkSize,z*chunkSize),new Quaternion(0,0,0,0)) as GameObject;
Chunk newChunkScript= chunks[x,y,z].GetComponent(\"Chunk\") as Chunk;
newChunkScript.worldGO=gameObject;
newChunkScript.chunkSize=chunkSize;
newChunkScript.chunkX=x*chunkSize;
newChunkScript.chunkY=y*chunkSize;
newChunkScript.chunkZ=z*chunkSize;
}
}
}
}
int PerlinNoise(int x,int y, int z, float scale, float height, float power){
float rValue;
rValue=Noise.GetNoise (((double)x) / scale, ((double)y)/ scale, ((double)z) / scale);
rValue*=height;
if(power!=0){
rValue=Mathf.Pow( rValue, power);
}
return (int) rValue;
}
// Update is called once per frame
void Update () {
}
public byte Block(int x, int y, int z){
if( x>=worldX || x<0 || y>=worldY || y<0 || z>=worldZ || z<0){
return (byte) 1;
}
return data[x,y,z];
}
}