- Home /
Need help getting screen to fade when loosing last life
I'm trying to get the screen to fade and transition to the game over screen. I already created a scrip that fades but only when I start a new game, restart and exit.
public class ScreenFader : MonoBehaviour {
// Holds the texutre to fill the screen
[SerializeField] Texture2D fadeTexture;
[SerializeField]Color colorStart = Color.black;
[SerializeField]Color colorEnd = Color.clear;
// Time to completion of fade
[SerializeField]float fadeTime = 1.0f;
//Internally used to track the passage of time
private float timer = 0.0f;
// Defines the type of function which is used as a
// delegate when the fade has completed
public delegate void FadeCompleteEvent();
// The container for the functions to be triggered when
// the fade has completed
private event FadeCompleteEvent OnfadeComplete = null;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
// Accumulate the passage of time
timer += Time.fixedDeltaTime;
if (timer > fadeTime)
{
timer = fadeTime;
}
if (OnfadeComplete != null)
{
OnfadeComplete();
}
OnfadeComplete = null;
Destroy(gameObject, 0.1f);
}
// Called when the GUI is drawn
void OnGUI() {
// Determine the passage of time as a
// percentage of the duration time
float percent = timer / fadeTime;
// Use the percentage to determine the
// gradient between the two
GUI.color = Color.Lerp(colorStart, colorEnd, percent);
// Create a rectangle the size of the screen
Rect screenRectangle = new Rect(0, 0, Screen.width, Screen.height);
GUI.DrawTexture(screenRectangle, fadeTexture);
}
// Loads the prefab and creates an instance from it
static public void CreateFade(string _prefabName,
FadeCompleteEvent _completeEvent) {
// Load the asset as a basic object
Object fadePrefab = Resources.Load(_prefabName, typeof(Object));
// Create a new game object from the loaded prefab
GameObject fadeObject = Instantiate(fadePrefab) as GameObject;
// Find the fader's script component and the assign the event
ScreenFader fader = fadeObject.GetComponent<ScreenFader>();
fader.OnfadeComplete = _completeEvent;
}
}
Here's the player scrip where it takes the player to the game over screen
if (CurLives == 0) {
Application.LoadLevel("GameOverScreen");
}
Comment