Search Unity

Question Help with child Transparency?

Discussion in 'Scripting' started by AerionXI, Apr 8, 2021.

  1. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    482
    Good afternoon! So I'm trying to set the children attached to my player object go transparent just like the parent. Problem is, I can only get the parent to be transparent & not the children.

    Code (CSharp):
    1.  
    2. static void SetAlpha ( Renderer r, float alpha ) {
    3.     var c = r.material.color;
    4.     c.a = alpha;
    5.     r.material.color = c;
    6. }
    7. public void SetAlphaRecursively ( Transform target, float alpha ) {
    8.     var renderers = target.GetComponentsInChildren <Renderer> ( );
    9.     Debug.Log ( $"Updating {renderers.Length} renderers" );
    10.     foreach ( var r in renderers ) {
    11.         SetAlpha ( r, alpha );
    12.     }
    13. }
    14. public void SetStandardMatAlpha ( Renderer objectRenderer, float a ) {
    15.     if ( objectRenderer != null ) {
    16.         SetAlphaRecursively ( target, distancePercentage );
    17.     }
    18. }
    19. private void ObjectTransparency ( Renderer objectRenderer, float distancePercentage = 0.0f ) {
    20.     if ( objectRenderer != null ) {
    21.         SetAlphaRecursively ( target, distancePercentage );
    22.     }
    23. }
    24.  
    Any help is kindly appreciated!

    Thank you & have a great afternoon!
     
  2. alexeu

    alexeu

    Joined:
    Jan 24, 2016
    Posts:
    257
    Hi,
    Are you sure you acceed to the child material ?

     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Make sure you are using a transparent rendering mode if it's the Standard shader - or else make sure you are using a shader that supports transparency.