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

Changing the alpha of child objects

Discussion in '2D' started by CaringKarol, Aug 22, 2019.

  1. CaringKarol

    CaringKarol

    Joined:
    Jan 22, 2018
    Posts:
    28
    In my game, whenever the player hits an enemy, they become invulnerable for a few seconds, causing them to be slightly transparent. I used unity's 2D animaton package to rig my character and animate it. I can't figure out how to make the sprites within the object become transparent. Could someone help me?
     
  2. eyee

    eyee

    Joined:
    May 11, 2013
    Posts:
    5
    You could get all SpriteRender Components from the animated object and change the material color to something transparent and back.

    Something like the following should work:

    Code (CSharp):
    1. var spriteRenderes = this.GetComponentsInChildren<SpriteRenderer>();
    2.         foreach (SpriteRenderer sr in spriteRenderes)
    3.         {
    4.             sr.sharedMaterial .color = new Color(1,1,1,0.5f);
    5.         }
     
  3. CaringKarol

    CaringKarol

    Joined:
    Jan 22, 2018
    Posts:
    28
    It's not working, sadly.

    Code (CSharp):
    1. public class Invulnerability : MonoBehaviour
    2. {
    3.     [SerializeField]
    4.     private float seconds;
    5.  
    6.     //Obtain health info
    7.     Health health;
    8.  
    9.     //Function to be invulnerable
    10.     public IEnumerator Invulnerable()
    11.     {
    12.         //Make player ignore enemy layer to not get hurt
    13.         Physics2D.IgnoreLayerCollision(9, 12, true);
    14.  
    15.         //Invulnerable
    16.         SetAlpha(.5f);
    17.         yield return new WaitForSeconds(seconds);
    18.  
    19.         //No longer maker player invulnerable
    20.         Physics2D.IgnoreLayerCollision(9, 12, false);
    21.         SetAlpha(1f);
    22.     }
    23.  
    24.     public void SetAlpha(float alpha)
    25.     {
    26.         var children = this.GetComponentsInChildren<SpriteRenderer>();
    27.  
    28.         foreach (SpriteRenderer child in children)
    29.         {
    30.             child.sharedMaterial.color = new Color(1, 1, 1, alpha);
    31.         }
    32.     }
    33.  
    34. }
    above is what my code looks like. It works when the object itself has a sprite renderer, but when I tried putting a sprite renderer on the empty object (the parent of the rigged sprite), it didn't do anything.
     
  4. CaringKarol

    CaringKarol

    Joined:
    Jan 22, 2018
    Posts:
    28
    I also tried making a public array of sprite renderers and placed my sprites in that array, but that didn't work either
     
  5. eyee

    eyee

    Joined:
    May 11, 2013
    Posts:
    5
    Oh I am sorry it’s child.color, the SpriteRenderer has it. This should work.
     
  6. CaringKarol

    CaringKarol

    Joined:
    Jan 22, 2018
    Posts:
    28
    It still refuses to work : (