Yash Raj
Yash Raj

Reputation: 11

Unity Editor crashes on running these scripts

So, my editor keeps on crashing on running these scripts. Specifications of my pc(if needed): Intel i5,8GB ram,Windows 10

I am trying to make a Minecraft game and my editor crashes:

VoxelData.cs:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public static class VoxelData
{
    public static readonly int ChunkWidth = 2;
    public static readonly int ChunkHeight = 2;
    public static readonly Vector3[] voxelVerts = new Vector3[8] {
        new Vector3(0.0f,0.0f,0.0f),
        new Vector3(1.0f,0.0f,0.0f),
        new Vector3(1.0f,1.0f,0.0f),
        new Vector3(0.0f,1.0f,0.0f),
        new Vector3(0.0f,0.0f,1.0f),
        new Vector3(1.0f,0.0f,1.0f),
        new Vector3(1.0f,1.0f,1.0f),
        new Vector3(0.0f,1.0f,1.0f),
};
    public static readonly int[,] voxelTris = new int[6, 6]{
        {0,3,1,1,3,2},
        {5,6,4,4,6,7},
        {3,7,2,2,7,6}, // top face
        {1,5,0,0,5,4},
        {4,7,0,0,7,3},
        {1,2,5,5,2,6}

        };

}

and the Chunk.cs script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Chunk : MonoBehaviour
{
    public MeshRenderer meshRenderer;
    public MeshFilter meshFilter;
    // Start is called before the first frame update
    int vertexIndex = 0;
    List<Vector3> vertices = new List<Vector3>();
    List<int> triangles = new List<int>();
    List<Vector2> uvs = new List<Vector2>();
    void Start()
    {

        for(int y = 0; y < VoxelData.ChunkHeight; y++)
        {
            for (int x = 0; x < VoxelData.ChunkWidth; x++)
            {
                for (int z = 0; x < VoxelData.ChunkWidth; z++)
                {
                    AddVoxelDataToChunk(new Vector3(x, y, z));
                }
            }
        }

        CreateMesh();

    }
     void AddVoxelDataToChunk(Vector3 pos)
    {
        for (int p = 0; p < 6; p++)
        {
            for (int i = 0; i < 6; i++)
            {
                int triangleIndex = VoxelData.voxelTris[p, i];
                vertices.Add(VoxelData.voxelVerts[triangleIndex] + pos);
                triangles.Add(vertexIndex);

                uvs.Add(Vector2.zero);

                vertexIndex++;
            }
        }
    }
    void CreateMesh()
    {
        Mesh mesh = new Mesh();
        mesh.vertices = vertices.ToArray();
        mesh.triangles = triangles.ToArray();
        mesh.uv = uvs.ToArray();

        mesh.RecalculateNormals();
        meshFilter.mesh = mesh;
    }
    // Update is called once per frame
}

why does it keep crashing, it is quite annoying and not letting me continue on this project. Please help and advise what to do

Upvotes: 0

Views: 121

Answers (1)

derHugo
derHugo

Reputation: 90580

The most inner

             // | here !
             // v 
for (int z = 0; x < VoxelData.ChunkWidth; z++)
{
    AddVoxelDataToChunk(new Vector3(x, y, z));
}

will cause an infinite loop since x is nowhere changed inside it.

It should most probably be z

             // | here !
             // v 
for (int z = 0; z < VoxelData.ChunkWidth; z++)
{
    AddVoxelDataToChunk(new Vector3(x, y, z));
}

Upvotes: 4

Related Questions