0
I am trying to do a combine block script. However, i encountered 2 problems: the blocks / the combined mesh isn’t showing up anymore, and when it was, all the blocks where at the same location (even tho the blocks are not showing up, i still ask to display the locations in the Debug Log and they are all the same). I check the locations at multiple places in the script, and i don’t see why they changed.
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class ThisWorldGen : MonoBehaviour {
public GameObject meshy;
public GameObject blockPrefab;
public GameObject grass;
public GameObject dirt;
public GameObject rock;
private string[] blockTag;
private GameObject stockin;
public int amp;
public int freq;
private Vector3 mypos;
[SerializeField]
private Material material;
private Vector3 blockLocations;
public List<MeshFilter> meshes = new List<MeshFilter>();
void Start()
{
Generate();
}
void Generate()
{
amp = 2;//Random.Range(0,100);
freq = 2;//Random.Range(30,100);
mypos = this.transform.position;
int cols = 100;
int rows = 100;
float startTime = Time.realtimeSinceStartup;
#region Create Mesh Data
MeshFilter blockMeshes = Instantiate(blockPrefab, Vector3.zero, Quaternion.identity).GetComponent<MeshFilter>();
for(int x = 0; x < cols;x++)
{
for(int z = 0; z < rows;z++)
{
float y = Mathf.PerlinNoise ((mypos.x + x) / freq,(mypos.z + z)/freq) * amp;
y = Mathf.Floor(y);
int a1 = 0;
for(float hy = y; hy > 0.0F; hy--)
{
blockMeshes.transform.position = new Vector3(mypos.x +
x,y,mypos.z +z);
Debug.Log(blockMeshes.gameObject.transform.position);//the
blocks positions are normal (all different)
meshes.Add(blockMeshes);
Debug.Log(meshes[a1].gameObject.transform.position);//the
blocks positions are normal (all different)
}
}
}
int i = 0;
MeshFilter[] listMeshes = new MeshFilter[meshes.Count];
while(i < (meshes.Count))
{
listMeshes _= meshes*;*_
Debug.Log(meshes*.gameObject.transform.position);//the blocks*
position are all the same (99.0,1.0,89.0)
Debug.Log(listMeshes*.gameObject.transform.position);//the blocks*
position are all the same (99.0,1.0,89.0)
i++;
}
CombineInstance[] combine = new CombineInstance[listMeshes.Length];
int w = 0;
while(w < listMeshes.Length)
{
Debug.Log(listMeshes[w].gameObject.transform.position);//the blocks
position are all the same (99.0,1.0,89.0)
combine[w].mesh = listMeshes[w].sharedMesh;
combine[w].transform = listMeshes[w].transform.localToWorldMatrix;
Destroy(listMeshes[w].gameObject);
w++;
}
Mesh finalMesh = new Mesh();
finalMesh.CombineMeshes(combine);
meshy.GetComponent().sharedMesh = finalMesh;
meshy.SetActive(true);
#endregion
Debug.Log(“Loaded in " + (Time.realtimeSinceStartup - startTime) + " Seconds.”);
}
}
so my two questions are: How do i get to display the cubes, and why are they all placed at 99.0,1.0,89.0 for no reason ?