Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Fade in object?

Discussion in 'General Discussion' started by nverback, Oct 2, 2015.

  1. nverback

    nverback

    Joined:
    Sep 24, 2015
    Posts:
    2
    I just started with Unity a few weeks ago. This probably has a very simple solution, but I cannot find an answer I could understand on the web. I want to fade out an object. The answers I found so far mentions about shaders and alpha channels, but I don't understand what it means. Help is appreciated!
     
  2. spryx

    spryx

    Joined:
    Jul 23, 2013
    Posts:
    453
    A sprite or a mesh? You can face both via alpha tweening. Scripting is slightly different though.
     
  3. nverback

    nverback

    Joined:
    Sep 24, 2015
    Posts:
    2
    Yeah, I meant in script. And just a sprite/object.
     
  4. calmcarrots

    calmcarrots

    Joined:
    Mar 7, 2014
    Posts:
    681
    Simple code. Use the first line as a variable and then the second in some sort of controlled environment such as an if statement inside a the Update loop.

    Code (csharp):
    1. SpriteRenderer sprRenderer = GetComponent<SpriteRenderer>();
    2. spriteRenderer.color = Color.Lerp(spriteRenderer.color, Color.clear, 3f * Time.deltaTime);
    Just a simple base. Don't use this code exactly as it won't get what you are looking for. Modify it a bit to make it fade in both ways. Maybe optimize somehow by only allowing the color to shift under certain conditions. Obviously only use GetComponent once in your Awake function.
     
unityunity