GUITexture Fade in and Out Hack Question

Hi,

I’ve messed up Zerofractal’s FadeIn script. Obviously I don’t understand the boolean functions…I’m hoping someone here can help straighten me out.

What I want to happen is when the “Jump” button is pressed two things happen 1) The GUITexture fades up (which it does) and 2) The MouseLook script for the camera is disabled (which it does). Unfortunately, when the “Jump” button is released, the GUITexture fades out like it’s supposed to, but MouseLook doesn’t turn back on. Here’s my code:

var buttonName="Jump";
var fadeDuration:float=0.5;
var initialDelay:float=5;
private var timeLeft:float=0.5;
 
function Awake () {
   timeLeft = fadeDuration;
}
 
function Update () {
   if (initialDelay > 0){
      initialDelay = initialDelay-Time.deltaTime;
   } else {
      if (Input.GetButton (buttonName))
         fade(true); 
      else
         fade(false);  	
   }
}
 
function fade(direction:boolean){
   var alpha;
   if (direction){
      if (guiElement.color.a < 0.5){
         timeLeft = timeLeft - Time.deltaTime;
         alpha = (timeLeft/fadeDuration);
         guiElement.color.a=0.5-(alpha/2);
         var go = GameObject.Find("MainCameraParentGameObject"); 
  		 go.GetComponent("MouseLook").enabled = false;   
      } else {
         timeLeft = fadeDuration;
      }
   } else {
      if (guiElement.color.a > 0){
         timeLeft = timeLeft - Time.deltaTime;
         alpha = (timeLeft/fadeDuration);
         guiElement.color.a=alpha/2;
      
      } else {
         timeLeft = fadeDuration;
      }
   }
}

Try this

var buttonName="Jump"; 
var fadeDuration:float=0.5; 
var initialDelay:float=5; 
private var timeLeft:float=0.5; 
  
function Awake () { 
   timeLeft = fadeDuration; 
} 
  
function Update () { 
   if (initialDelay > 0){ 
      initialDelay = initialDelay-Time.deltaTime; 
   } else { 
      if (Input.GetButton (buttonName)) 
         fade(true); 
      else 
         fade(false);      
   } 
} 
  
function fade(direction:boolean){ 
   var alpha; 
   if (direction){ 
      if (guiElement.color.a < 0.5){ 
         timeLeft = timeLeft - Time.deltaTime; 
         alpha = (timeLeft/fadeDuration); 
         guiElement.color.a=0.5-(alpha/2); 
         var go = GameObject.Find("MainCameraParentGameObject"); 
         go.GetComponent("MouseLook").enabled = false;    
      } else { 
         timeLeft = fadeDuration; 
      } 
   } else { 
      if (guiElement.color.a > 0){ 
         timeLeft = timeLeft - Time.deltaTime; 
         alpha = (timeLeft/fadeDuration); 
         guiElement.color.a=alpha/2; 
         var go = GameObject.Find("MainCameraParentGameObject"); 
         go.GetComponent("MouseLook").enabled = true; 
      } else { 
         timeLeft = fadeDuration; 
      } 
   } 
}

Thanks but no go. I actually tried that earlier, but it says:

errorBCE0067: There is already a local variable with the name ‘go.’

It also doesn’t work, if you leave out the second var go= statement.

Easy fix, just avoid the double-creation of the local variable go:

function fade(direction:boolean){ 
   var alpha; 
   var go = GameObject.Find("MainCameraParentGameObject"); 
   if (direction){ 
      if (guiElement.color.a < 0.5){ 
         timeLeft = timeLeft - Time.deltaTime; 
         alpha = (timeLeft/fadeDuration); 
         guiElement.color.a=0.5-(alpha/2);
         go.GetComponent("MouseLook").enabled = false;    
      } else { 
         timeLeft = fadeDuration; 
      } 
   } else { 
      if (guiElement.color.a > 0){ 
         timeLeft = timeLeft - Time.deltaTime; 
         alpha = (timeLeft/fadeDuration); 
         guiElement.color.a=alpha/2; 
         go.GetComponent("MouseLook").enabled = true; 
      } else { 
         timeLeft = fadeDuration; 
      } 
   } 
}

Cool! Thanks HiggyB…muy helpful!

You’re welcome, although thanks go to Daniel as well for his part in the effort. :slight_smile: