character fading on hit?

this is something im looking to do rather than a healthier, which i have.

so instead of the character just dying when the health bar reaches a point, he fades out. now i have something for the health bar, works perfectly, by all means use it.
but how can i use this to affect the players alpha?

here is what i have just now. i have tried playing with it, but i run into errors. have not seen anybody else ask this. so thought i may as well be the first :wink:

public float health = 100f;
    public float healthTimer;

    private SpriteRenderer healthBar;
    private Vector3 healthScale;

    void Start () {
        healthBar = GameObject.Find ("HealthBar").GetComponent<SpriteRenderer> ();
        healthScale = healthBar.transform.localScale;
    }

    void FixedUpdate () {
        if(health <= 0){
            Destroy(this.gameObject);
        }
        if(health > 100){
            health = 100;
        }
        HealthUpdate();
    }

    void HealthUpdate(){
        healthBar.transform.localScale = new Vector3(healthScale.x * health * 0.01f, 1, 1);
    }

    void OnCollisionEnter2D(Collision2D enemyHit){
        if (enemyHit.gameObject.tag == "Enemy"){
            health -= 3;
        }

        if(enemyHit.gameObject.tag == "HealthPickUp"){
            health += 10;
        }
    }

Grab the Renderer component and change the alpha value on the material being used. Make sure youโ€™re using a transparent shader for the object.

Something like this should set it to half transparency:

Renderer renderer = gameObject.GetComponent<Renderer>();
renderer.material.color.a = 0.5f;

ill give that a shot thanks. was trying until 2am, but thought that i should really get some sleep :wink: