Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Loop over all parts of a model and change alpha

Discussion in 'Scripting' started by foxvalleysoccer, Sep 28, 2017.

  1. foxvalleysoccer

    foxvalleysoccer

    Joined:
    May 11, 2015
    Posts:
    108
    I want to loop through all the parts of my model and change the alpha for each part with a renderer.

    Is that possible and how?
    This is what I'm thinking.

    Code (CSharp):
    1.  
    2. Color32 col;
    3.         foreach (Renderer child in outerEngineModel.GetComponent<Renderer>())
    4.         {
    5.             col = child.GetComponent<Renderer>().material.GetColor("_Color");
    6.             col.a = 255;
    7.             child.GetComponent<Renderer>().material.SetColor("_Color", col);
    8.            
    9.             foreach (Renderer childnested in child.GetComponent<Renderer>())
    10.             {
    11.                 col = childnested.GetComponent<Renderer>().material.GetColor("_Color");
    12.                 col.a = 255;
    13.                 childnested.GetComponent<Renderer>().material.SetColor("_Color", col);
    14.             }
    15.         }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,798
    You could also just use GetComponentsInChildren<Renderer>() (at the root object outerEngineModel) to come up with an array of Renderers (generally only done at startup), then iterate them as a simple array. That will work no matter how crazily nested your object hierarchy is.
     
    MiladZarrin1 and MaxGuernseyIII like this.
  3. MiladZarrin1

    MiladZarrin1

    Joined:
    Jul 7, 2013
    Posts:
    78
    The correct answer is what Kurt-Dekker told you. But if you ever need to do this on your own (probably for similar matters) you need to write a recursive function. It's a function that calls itself. something like this:

    Code (CSharp):
    1. void ChangeAllRenderersAlpha(GameObject o)
    2. {
    3.     // Do whatever you need to do with this object o
    4.  
    5.     foreach (GameObject child in o.GetChildren())
    6.     {
    7.         // it call itself again with all of its children,
    8.         // and the children will call it for any of their children and so on
    9.         ChangeAllRenderersAlpha(child);
    10.     }
    11. }
    I hope it's useful.
     
    foxvalleysoccer and Kurt-Dekker like this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,798
    Remember: to understand recursion, you must first understand recursion.
     
    MiladZarrin1 likes this.
  5. foxvalleysoccer

    foxvalleysoccer

    Joined:
    May 11, 2015
    Posts:
    108
    Thanks for this. I never thought about coding this way. I got it to work using the recursion.