Javascript Camera Fade Delay

Hi guys. I’ve tried searching the forums and countless coding websites, however, I can’t seem to find the solution. I’m trying to get the script to wait before it fades out suddenly, I don’t want it fading out slowly over the scene. Here’s my code:

var theTexture : Texture2D;
function DoEvent(){
      yield WaitForSeconds(10);
      OnGUI();
}

function OnGUI()
{
 GUI.color.a = Mathf.Lerp(0.0, 1.0, (Time.time / 2));
 GUI.DrawTexture(Rect(0,0,Screen.width, Screen.height), theTexture);
}

I appreciate you taking a look. :sunglasses:

Well, it makes no sense that you call the function OnGUI with in the DoEvent function. OnGUI is already called every 33 milliseconds, or something like that. Change the function. As you have it, your yield serves no purpose.

You’re using Time.time without ‘normalizing’ it; your GUI will always fade out after two seconds.

Your words confused me, but look at this and see if you can extract a solution:

var guicolor : Color;
function GUIFadeOut( seconds : float ) {
  var guiAlpha : float = 1.0;
  while ( guiAlpha > 0 ) {
    guiAlpha -= Time.deltaTime / seconds;
    guicolor.a = guiAlpha;
    yield;
  }
  guicolor.a = 0;
}

function OnGUI() {
  GUI.color = guicolor;
  DrawTexture(//);
}

I won’t pretend to really understand Javascript. I’m really at a loss here. I can’t figure out how to make it wait for x seconds before fading.

If you are having trouble, start more simply.

To get a good concept of how to make it wait for two seconds… Put this in a JavaScript at start.

function Start()
{
print("this just happened");
yield WaiForSeconds(2.0);
print("that just happened");
}

Open up the console, in the windows menu and watch. Once you learn that, try some other stuff. Seems to me you are jumping the gun a bit.

I ALWAYS jump the gun. But if I can get a solution, that’s fine. I figured my experience in other languages would help me here, but Unity seems to have taken Javascript and jumbled it all up…

It’s not Javascript, it’s Unityscript. They call it Javascript 'cause it has some similarities and “Javascript” is more attractive to new users than “Unityscript” (there are threads discussing this waaaay beyond what I can say on it).

Think of it more as a far more lenient alternative to C#.