Changing sprite alpha?

I’ve seen a few people ask this question on Answers, but I can’t get it working.

I want to smoothly decrease and increase sprites’ alpha value, based on trigger entering and exiting.

Here’s the script I’ve got so far:

    bool insideBuilding;
    public SpriteRenderer houseRenderer;
    public SpriteRenderer groundRenderer;
    Color houseColor;
    Color groundColor;
    public float transitionSpeed;

    void Start () {
        //Setting color variables to current renderer color values.
        Color houseColor = houseRenderer.color;
        Color groundColor = groundRenderer.color;

        //Setting renderer color values to color variables.
        houseRenderer.color = houseColor;
        groundRenderer.color = groundColor;
    }

    void OnTriggerEnter2D () {
        //Smoothly decrease alpha value.
        houseColor.a = Mathf.Lerp (255f, 0f, transitionSpeed);
        groundColor.a = Mathf.Lerp (255f, 0f, transitionSpeed);
    }

    void OnTriggerExit2D () {
        //Smoothly increase alpha value.
        houseColor.a = Mathf.Lerp (0f, 255f, transitionSpeed);
        groundColor.a = Mathf.Lerp (0f, 255f, transitionSpeed);
    }

I’m 99% sure I’m doing something obviously wrong, but I’ve never worked with sprite color in code.

The way I understand that is:

  • I’m setting both Color variables to be equal to their respective renderer’s values.
  • I’m setting those renderer’s values to those variables, so that they can be altered.
  • I’m changing the Color variables’ alpha value, which should in turn change the renderers’ values.

So in my mind is should work (but obviously it doesn’t). I’m not getting any errors at all, or warnings, but it just doesn’t change the alpha value.

Couple things, here. You were correct in that you need a variable to change the color, but what you do is make the variable equal to the renderer color, then change that variable, then make the renderer color equal to the changed variable.

The other thing that won’t work is Lerp needs a loop to function. It either needs to be in an update, or in a loop in a coroutine. The trigger is a one off kind of thing.

1 Like

How could I implement the Lerp into the coroutine?

I don’t use them much but your variable is changing so use a while loop or something:

while(houseColor.a >=0){
houseColor.a = Mathf.Lerp (0f, 255f, transitionSpeed);
houseRenderer.color = houseColor;
yield return new waitforseconds(time);//look this up because I can't remember the syntax

}

I have no idea if that will work or not, I just know it needs to be in a loop. Good luck. I don’t think it will break out of the loop at zero, so you would have to do something about that.

It doesn’t seem to be doing anything.

Would it work if I made the “OnTrigger” events set a boolean value, and then based on that boolean, do the alpha changes in Update?

I tried doing that, but so far it doesn’t seem to be working. The color just changed to black, and the alpha stays the same. And the color changes right at the start, even though I’m not altering anything other than the alpha.

I also tried turning the Color variables to floats, so the only information I would be handling, would be the alpha value for the renderers. But that doesn’t seem to be working either.

Did you use a coroutine? The first thing to do is to just try setting the alpha instantly to zero in the trigger to make sure that part is working. Next, use a coroutine or update to lerp it. It’s not going to matter if you use update or a coroutine, that’s up to you.

1 Like

Well in that case, I’ll stick to using Update.

Here’s the current version of the code:

    bool insideBuilding;
    public SpriteRenderer houseRenderer;
    public SpriteRenderer groundRenderer;
    public Color houseColor;
    public Color groundColor;
    public float transitionSpeed;
    bool isInTrigger;

    void Update () {

        if (isInTrigger == true) {
            //Smoothly decrease alpha values.
            houseColor.a = Mathf.Lerp (houseColor.a, 0f, transitionSpeed);
            groundColor.a = Mathf.Lerp (groundColor.a, 0f, transitionSpeed);

            houseRenderer.color = houseColor;
            groundRenderer.color = groundColor;
        }

        else {
            //Smoothly increase alpha values.
            houseColor.a = Mathf.Lerp (houseColor.a, 255f, transitionSpeed);
            groundColor.a = Mathf.Lerp (groundColor.a, 255f, transitionSpeed);

            houseRenderer.color = houseColor;
            groundRenderer.color = groundColor;
        }
    }

    void OnTriggerEnter2D () {
        isInTrigger = true;
    }

    void OnTriggerExit2D () {
        isInTrigger = false;
    }

The stuff in Start turned out to be useless, so I just removed the whole Start. I replaced the first numbers in the Lerps, with the Color variables’ alpha, so instead of going from the maximum to the minimum in the first one, it goes from the current to the minimum. And same for the second one except opposite.

The last else was also redundant, so I just made the whole thing a simple if/else, which also makes the code a whole lot easier to read.

Now when I enter the trigger, it smoothly turns down the alpha, but when I leave, it instantly jumps to full (255).

Any idea what might be causing that?

I’ve been staring at this code for almost the whole day. I’ve tried fidling with some stuff, like moving

houseRenderer.color = houseColor;
groundRenderer.color = groundColor;

around.

I’m completely clueless.

You’re lerping to 255 but alpha is clamped between 0 and 1. Therefore it takes 1/255th of the time it should to reach max alpha value.

While when you do it the other way around, it goes just fine since it’s lerping from 1 to 0, as intended.

1 Like

Thank you so much.

Any idea why that is? I mean, why is alpha clamped to 0-1 when changing it in script, even though the actual value in the color picker can go up to 255?

I don’t know! I want to say they’re normalized to ease the maths when using them. Not sure!

In all cases, Color32 ranges from 0-255.

1 Like