Transparent object (scripting)

Hi all!
I have a roof in my game and I want it to turn transparent when the player character walks under it. I have moved the collider of the roof down under it and knows that part works so therefore I need help with the transparent part. I have read several post with the solution: this.GetComponent().material.color.a = 0.05f; but have realized that this wont work due to the fact that the color.a is a Read Only variable. And because of this I wrote my code like this:

private Color colorOrig;
private Color colorTrans;
private Renderer r;

	void Start(){
		r = this.GetComponent<Renderer>();
		colorOrig = r.material.color;
		colorTrans = new Color (colorOrig.r, colorOrig.g, colorOrig.b, 0.5f);

	void OnTriggerEnter(Collider other) {
		if (other.tag == "Player") {
			r.material.color = colorTrans;

	void OnTriggerExit(Collider other) {
		if (other.tag == "Player")
			r.material.color = colorOrig;}

But it doesn’t work. Any suggestions?


I actually just completed this specific function for my game…
It is true that it is a read-only variable… all sub-variables within a vector are read only. To change it you must set it as it’s whole variable… (and not simply modify only the alpha)

Accessing the alpha variable is as follows:
(One of the many ways)

Color theColorToAdjust = renderer.material.color;
theColorToAdjust.a = 0f; // Completely transparent
theColorToAdjust.a = 1f; // completely opaque

*** ONCE you modify the sub-variable alpha, then you can SET IT by passing through the entire variable COLOR instead of only it’s ALPHA.
renderer.material.color = theColorToAdjust; //Which contains your new alpha setting

// If the color you want to adjust is on another different gameobject then you would....
Color theColorToAdjust = GameObject.FindGameObjectWithTag ("Player").GetComponent <Renderer>().material.color;
theColorToAdjust.a = 0f;

Since I have completed this entire thing, here is how I went about it…

  1. First I determine if something is standing in between my character and the camera. ( Is something like a roof in the way?). To do this; I made all of my roofs to be on a layer that I called “FadeLayer”. And then I simply did a physics.Raycast from the camera to my character and if my raycast touches anything that is on the FadeLayer, like a roof, then it will proceed to make it transparent…

    Vector3 direction = GameObject.FindGameObjectWithTag (“Player”).transform.position - transform.position;
    float distanceRay = Vector3.Distance (transform.position, GameObject.FindGameObjectWithTag (“Player”).transform.position);
    RaycastHit hitInfo;
    Physics.Raycast (transform.position, direction, out hitInfo, distanceRay);
    if (hitInfo.collider != null)

    		if (hitInfo.collider.gameObject.layer == 8) 
    			hitInfo.collider.gameObject.GetComponent <FadeFlag> ().GoFade ();

On every Roof or on every object that is supposed to “Go transparent”, I have this script attached to it… Please notice the last command in the code above is telling the FadeFlag scipts to “GoFade” which my function for initiating the transparency function.

** And so as long as the rooftop continues to receive the message to GoFade…Then it will continue to try to be transparent until it reaches full transparency… However, as soon as it is no longer getting a signal then ONLY AFTER 5 seconds, it will go back to being opaque.

Here is the FadeFlag script:

public bool receivedSignal = false;
public bool amITransparent = false;
public float signalTimeStamp;
public float returnDelay = 5f;
public float fadeIn = 0.05f;
public float fadeOut = 0.15f;

void FixedUpdate () 
		Color thisColor = renderer.material.color;
		if (thisColor.a < 1f) 
			amITransparent = true;
		} else 
				amITransparent = false;

		if (Time.realtimeSinceStartup >= signalTimeStamp + returnDelay && amITransparent == true) 
			// Begin making it opaque again because we haven't received a signal in over 5 seconds
			receivedSignal = false;
			thisColor.a = thisColor.a + fadeIn;
			if (thisColor.a >= 1f){thisColor.a = 1f;}
			renderer.material.color = thisColor;

		if (receivedSignal == true) 
			// then continue to fade to 100% transparency
			thisColor.a = thisColor.a - fadeOut;
			if (thisColor.a <0f){thisColor.a = 0f;}
			renderer.material.color = thisColor;


	public void GoFade ()
		receivedSignal = true;
		signalTimeStamp = Time.realtimeSinceStartup;


Your shader should be support color blending.