Create individual prefabs
Hi there,
I want to create a pointcloud. For this I have a Chunk class which can create voxel at any position. Because of the size of my cloud I have to seperate it into several chunks. Each chunk contains 8125 voxel. For this I created a world class where I instantiate a chunk and then I want to send it the coordiantes for the voxels. In someway it works and I can see two voxels but then I get "NullReferenceException: Object reference not set to an instance of an object" and I don't unterstand why. Here is my code:
From world script:
public GameObject chunkPrefab;
public GameObject newChunkObject;
public void CreateChunk(Vector3[] coordinates)
{
newChunkObject = Instantiate(chunkPrefab, new Vector3(0,0,0),
Quaternion.Euler(Vector3.zero)) as GameObject;
Chunk newChunk = newChunkObject.GetComponent<Chunk>();
newChunk.createBlockAt(0,0, 0);
}
From chunk script:
public virtual void createBlockAt(int x, int y, int z)
{
RenderMesh(Blockdata(x, y, z));
}
private MeshData Blockdata(int x, int y, int z)
{
meshData.vertices.Add(new Vector3(x - 0.5f, y + 0.5f, z + 0.5f));
meshData.vertices.Add(new Vector3(x + 0.5f, y + 0.5f, z + 0.5f));
meshData.vertices.Add(new Vector3(x + 0.5f, y + 0.5f, z - 0.5f));
meshData.vertices.Add(new Vector3(x - 0.5f, y + 0.5f, z - 0.5f));
meshData.vertices.Add(new Vector3(x - 0.5f, y - 0.5f, z - 0.5f));
meshData.vertices.Add(new Vector3(x + 0.5f, y - 0.5f, z - 0.5f));
meshData.vertices.Add(new Vector3(x + 0.5f, y - 0.5f, z + 0.5f));
meshData.vertices.Add(new Vector3(x - 0.5f, y - 0.5f, z + 0.5f));
meshData.AddQuadTriangles();
return meshData;
}
private void RenderMesh(MeshData meshData)
{
filter.mesh.Clear();
filter.mesh.vertices = meshData.vertices.ToArray();
filter.mesh.triangles = meshData.triangles.ToArray();
filter.mesh.RecalculateNormals()
}
the error always ends in RenderMesh method.
I Also tried to send my chunk along with the methods but nothing changed. Hope someone can help me. Thank you.
EDIT: My prefab contains a script which creates individual meshes. I can change the mesh by individual coordinates.
Answer by timetosmile · Oct 11, 2017 at 10:25 AM
Got it:
private void RenderMesh(MeshData meshData)
{
MeshFilter filter = GetComponent<MeshFilter>();
filter.mesh.Clear();
filter.mesh.vertices = meshData.vertices.ToArray();
filter.mesh.triangles = meshData.triangles.ToArray();
filter.mesh.RecalculateNormals();
}
Need to create the mesh in my function. In someway it is empty when I call it from world.