- Home /
 
 
               Question by 
               hollym16 · Dec 02, 2014 at 04:10 PM · 
                gameobjectmaterialtransparencyfade  
              
 
              Fading a GameObject in/out
I've seen some similar questions but they're all a bit specific.
What I want to achieve is to have a model that will slowly fade in then fade out again (over about 5 seconds) I've got the following script that can set the transparency:
 #pragma strict
 
 private var Cube : GameObject;
 Cube = GameObject.Find("Cube");
 
 function Start () {
 }
 function Update () {
 Cube.renderer.material.color.a = 0.5;
 }
 
               How would I go about setting the transparency to go from 0 to 1 then back to 0 over a period of time?
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by hollym16 · Dec 02, 2014 at 04:44 PM
I just found this script which works perfectly! You always find your answer when you stop looking for it:
 #pragma strict
  
  var duration : float = 1.0;
  var alpha : float = 0;
  
  function Update(){
  
      lerpAlpha();
  }
  
  function lerpAlpha () {
  
  var lerp : float = Mathf.PingPong (Time.time, duration) / duration;
  
      alpha = Mathf.Lerp(0.0, 1.0, lerp) ;
      renderer.material.color.a = alpha;
  }
 
              Your answer