Changing alpha of 2D game object when collided with

This is my first time ever using unity or C# so please be patient hahaha…
I’m trying to change the transparency/alpha of this table when the player collides with it, so you can see the player under the table.
(I’m learning through the Unity Essentials tutorial if u can’t already tell)

This is what I’ve come up with so far, but I don’t know where to go from here…
I want to make public variables so I can change the strength of the alpha and the speed in which it fades in/out.

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

public class TableTransparency : MonoBehaviour
{

    public float alpha = 1.0f;
    public float fadeSpeed = 0.1f;


    private void OnTriggerEnter2D(Collider2D other)
    {

        if (other.GetComponent<PlayerController2D>() != null)
        {
            transform.GetComponent<SpriteRenderer>().color -= new Color(0, 0, 0, fadeSpeed * Time.deltaTime);
        }

    }

}

update:
I still haven’t figured out how to fade, but I was able to make the alpha change! Except, I don’t know how to make it go back to normal when it’s exited.

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

public class TableTransparency : MonoBehaviour
{

    public float alpha = 1.0f;


    private void OnTriggerEnter2D(Collider2D other)
    {

        if (other.GetComponent<PlayerController2D>() != null)
        {
            transform.GetComponent<SpriteRenderer>().color -= new Color(0, 0, 0, alpha);
        }

    }

}

OnTriggerEnter2D only gets called once and so it’s not possible to keep fading with it.

Try this:

using UnityEngine;

public class TableTransparency : MonoBehaviour
{
    public float alpha = 1.0f;
    public float fadeSpeed = 1.5f;

    Color targetColor;

    SpriteRenderer sp;

    void Start()
    {
        sp=GetComponent<SpriteRenderer>();
        targetColor=Color.white;
    }

    void Update()
    {
        sp.color = Color.Lerp(sp.color, targetColor, fadeSpeed * Time.deltaTime);
    }

    void OnTriggerEnter2D(Collider2D other)
    {
        targetColor = new Color(1,1,1,0);
    }

    void OnTriggerExit2D(Collider2D other)
    {
        targetColor = Color.white;
    }
}

thanks so much! i had just come to that conclusion when you sent this message haha
the only thing i had to change in the code to get it to work was this:

    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.GetComponent<PlayerController2D>() != null)
        {
            targetColor = new Color(1, 1, 1, 0);
        }
    }

    void OnTriggerExit2D(Collider2D other)
    {
        if (other.GetComponent<PlayerController2D>() != null)
            targetColor = Color.white;
    }
}

otherwise it works like a charm! i’m not sure what a lot of this code even means but i’ll try to study from it

edit oh and i also had to change targetColor = new Color(1, 1, 1, 0); - i changed the 0 to var “alpha” to control it in engine.