The.Jack
The.Jack

Reputation: 21

Unity Fade delay when game ends

I'm fairly new at Unity and i'm trying to making a game. I want to have an subtitle fading in when you're at the end of the game. This start when you hit a button. But when I code a image that I fadein, it plays it directly when you start the game. Do you guys know a solution?

#pragma strict

private var guiShow : boolean = false;

var car : GameObject;
var rayLength = 10;
var guiObject : GUITexture;
var fadeTime = 1.0;
enum Fade {In, Out}
var fadesubtitles : boolean = false;


function Update ()
{
    var hit : RaycastHit;
    var fwd = transform.TransformDirection(Vector3.forward);

    if(Physics.Raycast(transform.position, fwd, hit, rayLength))
    {
        if(hit.collider.gameObject.tag == "car")
        {
                guiShow = true;
                if(Input.GetKeyDown("e"))
                {
                    guiShow = false;
                }   

                else if(Input.GetKeyDown("e"))
                {
                    guiShow = false;
                }
        }               
    }
    else
    {
    guiShow = false;
    }
}




function OnGUI()
{
    if(guiShow == true)
    {
        GUI.Box(Rect(Screen.width / 2, Screen.height / 2, 150, 25), "Press F to escape");
         if(Input.GetKeyDown("f")){
                fadesubtitles = true;
             }
     }
}
if (fadesubtitles == true){
             yield FadeGUITexture(guiObject, fadeTime, Fade.In);
             yield WaitForSeconds(3.0);
             yield FadeGUITexture(guiObject, fadeTime, Fade.Out);
}

function FadeGUITexture (guiObject : GUITexture, timer : float, fadeType : Fade) {
     if (subtitles == true){
     var start = fadeType == Fade.In? 0.0 : 1.0;
     var end = fadeType == Fade.In? 1.0 : 0.0;
     var i = 0.0;
     var step = 1.0/timer;

       while (i < 1.0) {
         i += step * Time.deltaTime;
         guiObject.color.a = Mathf.Lerp(start, end, i)*.5;
         yield;
       }
     }
}   

Upvotes: 1

Views: 355

Answers (2)

axwcode
axwcode

Reputation: 7824

You can use iTween.

FadeFrom(GameObject target, Hashtable args)

Example:

iTween.FadeFrom(gameObject, iTween.Hash("alpha", 0f, "amount", 1f, "time", 2f));

Upvotes: 0

Eddie Parker
Eddie Parker

Reputation: 4888

I'd start your game object in the 'disabled' state (uncheck it in the inspector). Then at then end of the game, have some code that enables it.

Upvotes: 0

Related Questions