Accessing properties from variable

private var rend;

function Awake()
{
    if (gameObject.GetComponent(TextMesh) != null)
        rend = gameObject.GetComponent.<TextMesh>();

    else if (gameObject.GetComponent(SpriteRenderer) != null)
        rend = gameObject.GetComponent.<SpriteRenderer>();
}

Now if I want to use rend

rend.color.a = 0.5; // Gives an error: 'color' is not member of 'Object'

So I have to do it in this way:

(rend cast SpriteRenderer).color.a = 0.5; // if using SpriteRenderer

However, I have to do this maybe 100 times in my script and I have to always check which
type I’m using making all this pointless. The idea behind this is to use the same variable to change things regardless what type is placed to gameobject. So I could always use like rend.color.a, because both TextMesh and SpriteRenderer have color properties.

I’m not sure whether this has been bumped or UA’s had a timelapse, either way you’d be better off just storing the reference to Color rather than the GO

private var color : Color;

function Awake(){
  if(gameObject.GetComponent(TextMesh) != null){
    color = gameObject.GetComponent.<TextMesh>().color;
  }else if(gameObject.GetComponent(SpriteRenderer) != null){
    color = gameObject.GetComponent.<SpriteRenderer>().color;
  }

  color.a = 0.5;
}

Obviously keep the reference to rend too if needed.