- Home /
Why aren't the right amount of blocks instantiating?
In the followings script assume everything is defined properly. The BlockData.currentChunk variable holds the chunk positions. All chunk objects are instantiated at the start. (chunk set up is 6x1x6). I check if the distance between the player and the chunk is less than 20, and if it is I add the chunk to a list of Transforms named chunksToRender. Then i go into my renderBlocks function and cycle through each chunk in the list. It should only render blocks in the chunks in that list, which the debug.Log shows as 4 chunks. Therefore only 40 blocks (each chunk is 10x1x10), however it still renders all 400 of them. If anyone could help out I'd GREATLY appreciate it as this is something I REALLY want to figure out.
ALSO THE MAIN FOCUS IS THE CREATECHUNKS() FUNCTION AND RENDERBLOCKS() FUNCTION
using System.Collections.Generic;
public class terrain : MonoBehaviour {
public int blocksX = 20;
public int blocksY = 10;
public int blocksZ = 20;
public Transform block;
public Transform chunkPrefab;
public List<Transform> chunksToRender = new List<Transform>();
public Transform player;
public bool renderBlocks = false;
void Start () {
createChunks();
generateTerrainMaterials();
}
void Update() {
if(renderBlocks) {
renderBlock();
}
}
private void generateTerrainMaterials() {
foreach(Transform obj in BlockData.blockGO) { //cycle through each block
if(obj.transform.position.y >= 3) //assign grass material if block is a surface block
obj.renderer.material = (Material)Resources.Load ("Materials/grass");
}
}
private void renderBlock() {
int blocksRndered = 0;
BlockData.blockGO = new Transform[blocksX,blocksY,blocksZ];
foreach(Transform curChunk in chunksToRender) {
float curChunkX = curChunk.position.x;
float curChunkZ = curChunk.position.z;
for(int x = 0; x < blocksX; x++) {
for(int y = 0; y < blocksY; y++) {
for(int z = 0; z < blocksZ; z++) {
Vector3 blockPos = new Vector3(x * curChunkX, y, z * curChunkZ);
Transform blockPrefab = Instantiate(block, blockPos, Quaternion.identity) as Transform;
blockPrefab.renderer.material = (Material)Resources.Load("Materials/soil");
BlockData.blockGO[x,y,z] = blockPrefab;
blocksRndered++;
}
}
}
}
Debug.Log ("blocks rendered: " + blocksRndered);
renderBlocks = false;
}
private void createChunks() {
BlockData.currentChunk = new Transform[BlockData.chunkX, BlockData.chunkY, BlockData.chunkZ];
int numberOfChunks = 0;
for(int cx = 0; cx < BlockData.chunkX; cx++) {
for(int cy = 0; cy < BlockData.chunkY; cy++) {
for(int cz = 0; cz < BlockData.chunkZ; cz++) {
Vector3 chunkPos = new Vector3(blocksX * cx, blocksY * cy, blocksZ * cz);
Transform chunkInst = Instantiate(chunkPrefab,chunkPos, Quaternion.identity) as Transform;
BlockData.currentChunk[cx,cy,cz] = chunkInst;
if(Vector3.Distance(player.position,chunkInst.position) < 20) {
chunksToRender.Add(chunkInst);
}
numberOfChunks += 1;
}
}
}
renderBlocks = true;
Debug.Log("number of chunks: " + numberOfChunks + " chunks to render: " + chunksToRender.Count);
}
}
ALso the Debug.Log at the bottom of the script displays the correct number of chunks, and number of chunks to render, but it still renders more that amount.
Your answer
Follow this Question
Related Questions
Foreach loop not going through all the children 1 Answer
Having a strange problem with yield and instantiate (C#) 1 Answer
For Loop isn't working properly! 1 Answer
Instantiate inside foreach loop 1 Answer
Checking if object intersects? 1 Answer