- Home /
Fade In each level
Hi there
I am trying out fading in when I start each level of my game, but my problem is that it only happens in the the beginning of my game and I want to fade in each time I go to a different scene of my game. I can't figure out why it only load once in the beginning and ever again.
Here is my code:
var fadeTexture : Texture;
var startColor : Color = Color(0,0,0,1);
var endColor : Color = Color(0,0,0,0);
var duration : float = 2.0;
internal var currentColor : Color;
function Start () {
currentColor = startColor;
Destroy(gameObject, duration + 1);
}
function OnGUI() {
GUI.depth = -10;
GUI.color = currentColor;
GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), fadeTexture);
}
function FixedUpdate () {
currentColor = Color.Lerp(startColor, endColor, Time.time/duration);
}
What is that script attached to? When you load a new level, do you load it additively, or do you just use Application.LoadLevel?
The Script is attached to an empty gameObject inside each scene and I load it additively and not on Application.LoadLevel.
Answer by Bunny83 · Feb 20, 2013 at 10:16 AM
Time.time is the time from the beginning of your game. It doesn't restart when you load another scene. So use
private var startTime : float;
function Start()
{
// [...]
startTime = Time.time;
}
function FixedUpdate ()
{
currentColor = Color.Lerp(startColor, endColor, (Time.time-startTime)/duration);
}
Your answer
Follow this Question
Related Questions
Reduce 'Levels' or Scene File Size 1 Answer
Get name of Scene-file from MonoBehaviour 1 Answer
Shared canvas for some scenes 1 Answer
One Location Game (with Scenes Loaded and Disabled) 0 Answers