- Home /
Why is my timer not starting after reloading a level?
I have looked around for answers, but unfortunately have found none. I have a fade-in in my menu level based on a colored plane in front the main camera. This works fine the first time I load the game to the Main Menu, but when I go back to the Main Menu from the gameplay UI, the fade never starts, but the music and buttons are functioning nonetheless. Any help would be greatly appreciated! UPDATE: I know very little about Javascript, trying to learn it a little.
#pragma strict
var timer : float = 5.0f;
function Awake(){
}
function Start () {
renderer.material.color.a = 1;
}
function Update () {
timer = timer - Time.deltaTime;
if(timer > 0){
renderer.material.color.a -= 0.1 * Time.deltaTime;
}
if(timer <= 0){
timer = 0;
}
}
Answer by MakakWasTaken · Mar 29, 2015 at 10:30 AM
Try this instead:
function Update () {
renderer.material.color.a = Mathf.MoveTowards(renderer.material.color.a,0,Time.deltaTime * 0.1);
}
Thank you for your response. I tried the script change, it did work as far as the fade, but it still doesn't happen when I go back to the level the fade is supposed to occur in. The fade occurs in the main menu level. Here is what's going on.
Fade in to main menu.
Load gameplay level.
Quit to main menu (GUI Function).
$$anonymous$$ain $$anonymous$$enu buttons and music function, but the screen does not fade in, so I can't see them.
Try this ins$$anonymous$$d:
function OnLevelWasLoaded () {
Fade(true);
}
var isDone : boolean;
function LoadLevel(name : String) {
Fade(false);
while (!isDone) {
yield;
}
Application.LoadLevel(name);
}
function Fade(Out : boolean) {
isDone = false;
while (renderer.material.color.a != 1) {
if (Out) {
renderer.material.color.a = $$anonymous$$athf.$$anonymous$$oveTowards(renderer.material.color.a,0,Time.deltaTime * 0.1);
} else {
renderer.material.color.a = $$anonymous$$athf.$$anonymous$$oveTowards(renderer.material.color.a,1,Time.deltaTime * 0.1);
}
yield;
}
isDone = true;
}
I think the issue may be with the object itself. None of the booleans change except for Done, which always starts as True.
Scratch that, this section isn't being called:
if (Out) {
Debug.Log("occur");
renderer.material.color.a = $$anonymous$$athf.$$anonymous$$oveTowards(renderer.material.color.a,0,Time.deltaTime * 0.1);
}
When you want to load a level you have to use LoadLevel ins$$anonymous$$d of Application.LoadLevel
Answer by lcfr822 · Mar 29, 2015 at 04:34 PM
So this was all a stupid mistake, I had set Time.timescale to 0 in the pause menu, but had not set it back to 1 after clicking main menu. I fixed it by putting Time.timescale = 1f in function start(). Thank you for your help!