- Home /
Game Object's Vector3 changing threw script without reason
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[i] = meshes[i];
Debug.Log(meshes[i].gameObject.transform.position);//the blocks
position are all the same (99.0,1.0,89.0)
Debug.Log(listMeshes[i].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<MeshFilter>().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 ?
Answer by Bunny83 · Jun 11, 2019 at 01:50 AM
You create one single object here:
MeshFilter blockMeshes = Instantiate(blockPrefab, Vector3.zero, Quaternion.identity).GetComponent<MeshFilter>();
which you add several times into your meshes list here:
meshes.Add(blockMeshes);
So you end up with a list that contains several references to the exact same object.
then, why when i ask for all the locations at line 51 and 54, i get different locations ?
Because you check / log the position inside the loop right after you changed the position of that single gameobject. The actual position that your gameobject will have once your loop is completed is the last set position.
Your answer
Follow this Question
Related Questions
How do i replace a Mesh Filter?? 1 Answer
how to render 60k cubes as efficiently as possible? 1 Answer
How do i modify a mesh Filter(C#)? 2 Answers
Unity5 Procedural meshing slower than in Unity4? 1 Answer
Mirror vertices procedurally 2 Answers