Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Fade out enemy when he dies

Discussion in 'Universal Render Pipeline' started by riki1999riki, Apr 5, 2020.

  1. riki1999riki

    riki1999riki

    Joined:
    Feb 3, 2020
    Posts:
    7
    Hello,
    I am trying to slowly fade out enemies after they die. I somehow figured it out before like this: (material = original material on object instance)
    Code (CSharp):
    1. mat = new Material(Shader.Find("Standard"));
    2. mat.CopyPropertiesFromMaterial(material);
    3. int numOfChildren = zombieBody.transform.childCount;
    4. for (int i = 0; i < numOfChildren; i++)
    5. {
    6.     GameObject child = zombieBody.transform.GetChild(i).gameObject;
    7.     child.GetComponent<Renderer>().material = mat;
    8. }
    9.        
    10. for (float f = 1f; f >= -0.05f; f -= 0.05f)
    11. {
    12.         Color c = mat.color;
    13.         c.a = f;
    14.         mat.color = c;
    15.         yield return new WaitForSeconds(0.05f);
    16. }
    17. yield return new WaitForSeconds(1f);
    18. Destroy(zombie);
    Zombie is build up from more child 3D objects with the same material.

    But that worked with default render pipeline and Standard shader. Now I switched to Universal Render Pipeline and it is not working. I cannot use: mat = new Material(Shader.Find("Standard"));, because I do not know what string should I put there. Instead I wrote this: mat = new Material(material.shader);, but the zombie doesnt fade out, just dissapears when destroyied.

    Could someone please help me?
     
  2. Remy_Unity

    Remy_Unity

    Unity Technologies

    Joined:
    Oct 3, 2017
    Posts:
    695
    The shader name you are probably searching for is
    Universal Render Pipeline/Lit
    .

    Secondly, instead of creating a new shader and copying the properties, it would probably be better to simply copy the material using
    mat = Instantiate( material );


    Last of all, the URP/Lit shader uses _BaseColor as main color property name, this won't work with the
    mat.color
    accessor. You will have to use
    mat.GetColor("_BaseColor");
    and
    mat.SetColor("_BaseColor", c)
     
    Last edited: Mar 1, 2021
    pixelR, riki1999riki and hertz-rat like this.
  3. riki1999riki

    riki1999riki

    Joined:
    Feb 3, 2020
    Posts:
    7
    Thanks! Now it works perfectly.
     
  4. pixelR

    pixelR

    Joined:
    Sep 16, 2013
    Posts:
    58
    Actually, single quotes will cause character literal error, so the name should be enclosed in double quotes:
    ("_BaseColor").
     
  5. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,683
    Dude, are you serious? Necroposting because of THIS...
     
    felipemullen and Lapsapnow like this.