Setting transparency in script?

I am learning via youtube and reading, I know there is a c# line that goes right in here to set the transparency when i am changing the color, but I can find no examples or explanations via google and forums. This is the snippet of code that is all in all working properly, but i want to change the transparency as I am changing colors.

void Update ()
{
if (current)
{
GetComponent().material.color = Color.magenta;
}
else if (target)
{
GetComponent().material.color = Color.green;
}
else if (selectable)
{
GetComponent().material.color = Color.red;
}
else
{
GetComponent().material.color = Color.white;
}
}

I found this as a old example from 2010 but it did not work.

other.renderer.material.color.a = 0.5; // 50 % transparent

So that pretty much explains it sorry for the noob question, I searched all over the renderer.API stuff with no luck.

You don’t need to constantly get your renderer in update like that. Just name it and get it in start:

    Renderer renderer;
    void Start()
    {
        renderer = GetComponent<Renderer>();
    }

    void Update()
    {
        if (true)
        {
            renderer.material.color = Color.magenta;
            Color color = renderer.material.color;
            color.a = .3f;
            renderer.material.color = color;
        }
    }
}

You also need to set your material to transparent in the editor by clicking on the little arrow under Rendering Mode.

its for a turn based game so the tile colors change every turn based on what unit you have highlighted. Ill try this tho and see if it works thank you!!

In that case you should not use the update method. Instead you should send a message when the player clicks on hte object. I don’t want to confuse you too much if you just began but it never hurts to get the right direction.

I assume that at some point you assign the current variable for example if hte player clicks on it. This might be another script. Then you can do this:

//Put this on top of oyur manager class:
[SerializeField] Color NormalColor;
[SerializeField] Color CurrentColor;
[SerializeField] Color selectableColor;
[SerializeField] Color TargetColor;


//Wherever you assign states:

//Instead of
tile.current = true;
//Use this:
tile.SetColor(CurrentColor);

//Instead of
tile.target = true;
//Use this:
tile.SetColor(TargetColor);

//Instead of
tile.selectable = true;
//Use this:
tile.SetColor(SelectableColor);

//Make sure to deselect the old objects:
//Instead of
tile.current = false;
//Use this:
tile.SetColor(NormalColor);

YOu then need a method in your tile class called SetColor:

public void SetColor(Color color)
{
    renderer.material.color = color;
}

Did you perhaps mean “if (renderer != null)”, or?

He had some boolean in there, I just took a piece of the code to show how to do the things necessary. That’s kind of a problem with using code tags because it looks like it should be replacement code, but wasn’t meant for that purpose. It was just an example on how to show transparency.