Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by adnan0819 · Feb 23 at 03:47 AM · prefabtrianglesprefab changing at runtimecolor32

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.

alt text

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.

screen-shot-2022-02-22-at-54149-pm-min.png (367.1 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

173 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Dynamically Loading audio into a prefab and Instantiate in scene. 0 Answers

Is is possible to replace a prefab with a variant in script? 1 Answer

How to attach a Script/GameObject in the Scene view to a Prefab in the Assets folder. 1 Answer

Reinitialize prefab 0 Answers

How to change a your player look in once scene and then load it in another 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges