- Home /
Prefab array triangle colors being duplicated
Hi,
I am fairly new to Unity and I have an array of trees that I instantiate from a single Prefab. Then, I want to go through each "triangle" of the meshes of those trees (instantiated prefabs), and assign them Colors. What I am seeing is that all the trees are being the same color or likely copies of a "single" one instead of the trees in the arrays. Please see the ChangeColor method below. Maybe is is something fundamental about prefabs that I am missing.
These three trees should not have the same colors in every triangle according to what I am trying to accomplish.
The Start Function is below but I believe the only relevant part here is Instantiate. The rest is in ChangeColor().
void Start ()
{
counter = 0;
trees = new GameObject[numTrees];
for(int i = 0; i < numTrees; i++)
{
trees [i] = (GameObject)Instantiate (treePrefab, Vector3.zero, Quaternion.identity);
trees [i] = changeColor (trees [i],i);
}
List<GameObject> points = new List<GameObject>();
List<int> distances = new List<int>();
cam = GetComponent<Camera>();
for (int v = 0; v < vertices.Length; v++)
{
vertices[v].y = Mathf.PerlinNoise((vertices[v].x + this.transform.position.x)/detailScale,
(vertices[v].z + this.transform.position.z)/detailScale)*heightScale;
if(Random.Range(0,100)<1)
{
//GameObject newTree = TreePool.getTree();
GameObject newTree = this.getTree();
if(newTree != null)
{
//float x = (Random.insideUnitCircle.x * 5);
float dist_x = (Random.insideUnitCircle.x * 200); //OG is 20
float dist_z = (Random.insideUnitCircle.y * 200); //OG is 20
Vector3 treePos = new Vector3 (vertices [v].x + this.transform.position.x,
vertices [v].y - 1,
vertices [v].z + this.transform.position.z);//+dist_z);
newTree.transform.position = treePos;
}
ChangeColor
public GameObject changeColor(GameObject tree, int idx){
Mesh meshT = tree.GetComponent<MeshFilter>().mesh;
Vector3[] verticesT = meshT.vertices;
Color32 currentColor = new Color32();
int[] triangles = meshT.triangles;
//meshT.Clear ();
Color32[] colors = new Color32[triangles.Length];
//Debug.Log ("NUM OF TRI IS " + triangles.Length);
Vector3[] verticesModified = new Vector3[triangles.Length];
int[] trianglesModified = new int[triangles.Length];
int r=0;
int g=0;
int b=0;
for (int j = 0; j < triangles.Length; j++) {
// Makes every vertex unique
verticesModified[j] = verticesT[triangles[j]];
trianglesModified[j] = j;
b = counter++ % 256;
if (b==255){
g += 1;
g = g % 256;
}
if (g==255 && b==255){
r+=1;
r=r%256;
}
//the idx is just for debugging
if (idx % 2 == 0) {
r = r;
g = g;
b = b;
} else if (idx % 2 == 1){
int t=b;
r = t;
g = g;
b = r;
}
currentColor = new Color (
r,
g,
b,
255
);
//Debug.Log ("r = " + r + "g = " + g + "r = " + b);
counter += 1;
colors [j] = currentColor;
}
//counter += trianglesModified.Length;
// Applyes changes to mesh
meshT.vertices = verticesModified;
meshT.triangles = trianglesModified;
meshT.colors32 = colors;
//tree.GetComponent<MeshFilter> ().mesh.colors32 = colors;
tree.GetComponent<MeshFilter> ().mesh= meshT;
tree.SetActive(false);
return tree;
//trees[i].SetActive(false);
}
GetTree
public GameObject getTree()
{
for(int i = 0; i < numTrees; i++)
{
if(!trees[i].activeSelf)
{
return trees[i];
}
}
return null;
}
In summary, I cannot figure out why all the tree objects instantiated from the prefab in the trees array showing the same color while I am giving each triangle a unique color?
Thanks so much for your help in advance.
Your answer
Follow this Question
Related Questions
Prefabs spawn with wrong values 0 Answers
Object spawns itself instead of it's prefab. 3 Answers
How to edit prefab instances at Runtime/Creation Time 1 Answer
Prefabs won't change? 1 Answer
How to update a prefab from a copy in the hierarchy 0 Answers