Deactivate renderer when alpha is 0

Hello!

Recently i had to start working on a prototype of a new project in Unity, but i only understand the very basics of scripting and started doing this a few days ago.

So, i want my script to activate the renderer of object A and let it fade in if objectB enters its collider.
And if objectB leaves the collider, objectA shall fade out and deactivate its renderer again.

Object A is always a plane with a video or normal texture on it.

Everything works right now, except the deactivation of the renderer. :frowning:

Also, if you guys could tell me how i could use the collider of a 3. object to do this i would be very happy. (if B enters C = A fades in / if B leaves C = A fades out)
This would help me being able to adjust the collider much faster, because i found its faster to reposition a game object instead of a collider. Collider handles are a bit more annoying!

and heres the code:

 using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class planeFade : MonoBehaviour {


    bool fadeout = false;

    Renderer myRenderer;




    void Start ()
    {
        myRenderer = GetComponent<Renderer>();

    }

    void Update ()
    {
        if (fadeout) {
            myRenderer.material.color = Color.Lerp (myRenderer.material.color, new Color (myRenderer.material.color.r, myRenderer.material.color.g, myRenderer.material.color.b, 1f), 1 * Time.deltaTime);
        }else{
            myRenderer.material.color = Color.Lerp(myRenderer.material.color, new Color(myRenderer.material.color.r, myRenderer.material.color.g, myRenderer.material.color.b, 0f), 1*Time.deltaTime);
        }

        if (myRenderer.material.color.a == 0f) {
            myRenderer.enabled = false;
        }

    }

    private void OnTriggerEnter(Collider  other)
    {
        myRenderer.enabled = true;
        fadeout = true;
    }

    private void OnTriggerExit(Collider other)
    {
        fadeout = false;
    }
}

Alpha is a float value so it will almost never be an exact int value. Try <= instead of ==

Thanks for the reply. But unfortunately it still doesnt work.

Ah I see that you’re leaping wrong, also you don’t need 2lerps for this.
Leaping works like this:
you have two values A and B and you have one float lets call it t that should go from 0.0 to 1.0
If you lerp is lerp(A,B,t), when t = 0, your value will be 100% A, when t = 1, the value is 100% B
so t at 0.5 gives 50%A + 50%B.
Time.deltaTime just gives you how long it toke to render this frame, so 1*Time.deltaTime mostly gives you something around 0.03
For your lerp you should do something like

float lerp;
void Update()
{
    lerp += Time.deltaTime;
    Mathf.Clamp01(lerp);
    Color c = Color.Lerp(A,B,lerp);
}

And so lerping from 1 to 0 should give you the inverse effect.

Cool, thank you again. Ill try to make it work as you suggest. But still the renderer doesnt deactivate.

How do you start? With your renderer disabled, because I’m not sure if getcomponent works on disabled components.
Maybe set it public for debugging?
If you changed the lerp function and the 0 check to <=, then check if the materials alpha is actually changing.
Maybe the lerp brings it to 0.00something, then just check if <= 0.1 orso…

yeah, i start with the renderer disabled and the alpha at 0. When i enter, the renderer gets enabled and the alpha fades to 1.

When i change the check value to 0.1 the whole script stops working.

Edit:
Finally it works! Heres the code.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class planeFade : MonoBehaviour {


    float fadeout = 0f;

    Renderer myRenderer;




    void Start ()
    {
        myRenderer = GetComponent<Renderer>();

    }

    void Update ()
    {
        myRenderer.material.color = Color.Lerp (myRenderer.material.color, new Color (myRenderer.material.color.r, myRenderer.material.color.g, myRenderer.material.color.b, fadeout), 1 * Time.deltaTime);

        if (myRenderer.material.color.a <= 0.01f) {
            myRenderer.enabled = false;
        }

    }

    private void OnTriggerEnter(Collider  other)
    {
        myRenderer.enabled = true;
        fadeout = 1f;
    }

    private void OnTriggerExit(Collider other)
    {
        fadeout = 0f;
    }
}

still your lerp isn’t what i should be, Debug 1*Time.deltaTime and you will see that the value isn’t incrementing. at best it’s just twitching around some value.
you really should try doing something like this:

float lerp;
float lerpSpeed = 0.5f;
Color lerpColor;
bool isFading;
float [URL='http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=fadeout']fadeout[/URL] = 0f;
[URL='http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=Renderer']Renderer[/URL] myRenderer;

void Start()
{
    lerpColor = new [URL='http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=Color']Color[/URL] (myRenderer.material.color.r, myRenderer.material.color.g, myRenderer.material.color.b, [URL='http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=fadeout']fadeout[/URL]);
    myRenderer = GetComponent<Renderer>();
}

void Update()
{
    if(isFading)
    {
        lerp += Time.deltaTime *lerpSpeed;
        lerp = Math.Clamp01(lerp);
        myRenderer.material.color = [URL='http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=Color']Color[/URL].[URL='http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=Lerp']Lerp[/URL] (myRenderer.material.color, lerpColor, lerp);
        if (myRenderer.material.color.a <= 0.01f) 
        {
            myRenderer.[URL='http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=enabled']enabled[/URL] = false;
            isFading = false;
            fadeout = 0;
        }
    }
}

void OnTriggerEnter(Collider other)
{
    myRenderer.[URL='http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=enabled']enabled[/URL] = true;
    isFading = true;
    lerp = 0;
    [URL='http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=fadeout']fadeout[/URL] = 1f;
}

untested but 99% sure it works :wink:

Thanks again, i really appreciate your help!
I’m trying to get this running, but unfortunately the 1% unsureness resulted in 2 mistakes (at least 2 i think i figured out). :stuck_out_tongue_winking_eye:

The first one is easy to fix: its “Mathf”, not just “Math” in line 19.

The 2nd one is that you should switch line 10 and 11, or you get a “NullReferenceException”. I hope i understood it right and it was the right way to fix this one.

So now i get no errors, but i also found out that the script doesnt seem to work like this. Nothing happens, except that the renderer now deactivates when i enter the collider. And this isnt exacly what i was looking for. :slight_smile:

hahaha yes sorry, was to hasty…
here is a working and tested version:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Fader : MonoBehaviour {

    public GameObject planeToFade;
    public float lerpSpeed = 0.5f;
    bool isFading;
    float lerp;
    Color lerpColor;
    float fadeValue = 1f;
    Renderer myRenderer;
    
    void Start()
    {
        myRenderer = planeToFade.GetComponent<Renderer>();
        lerpColor = myRenderer.material.color;
    }
    
    void Update()
    {
        if(isFading)
        {
            lerp += Time.deltaTime *lerpSpeed;
            lerp = Mathf.Clamp01(lerp);
            myRenderer.material.color = new Color(lerpColor.r, lerpColor.g, lerpColor.b, Mathf.Lerp(myRenderer.material.color.a,fadeValue,lerp));
            if (lerp >= 0.99f)
            {
                if(myRenderer.material.color.a <= 0.01f)
                    myRenderer.enabled = false;
                isFading = false;
                fadeValue = 0;
            }
        }
    }
    
    void OnTriggerEnter(Collider other)
    {
        myRenderer.enabled= true;
        isFading = true;
        lerp = 0;
        fadeValue = 1f;
    }
    void OnTriggerExit(Collider other)
    {
        isFading = true;
        lerp = 0;
        fadeValue = 0f;
    }
}

put this on your trigger and give the planeToFade variable the gameObject you want to fade in and out.
make sure you set the shader of the plane to fade.

I’ll try it as soon as i can and give you feedback! :smile:

And you have nothing to apologise for, you’re a great help!

Its working perfectly! Exactly what i was looking for from the beginning.

Thank you very very much!

1 Like