user5663447
user5663447

Reputation:

Button doesn't work correct on scene fading

I'm facing a problem with scene fading. I made a animation of fade in & out also made a script named as Fader which has a coroutine function. Animation works fine. There is also a empty game object named as SceneManager which has a script. In this script button functions are written which open a scene. enter image description here

enter image description here

But the problem is when i click any button,for example Scene2 Button then fade in animation start,at some milliseconds when black screen appear,if i click on this black screen then another scene is open. It won't open scene2 Watch this video

https://drive.google.com/file/d/0B1H5fdK2PJAnbm5fWDhlN3dVVnc

package link https://drive.google.com/file/d/0B1H5fdK2PJAnZ2Y1UEFRMmVFbTA

Scene Manager script:

public class Manager : MonoBehaviour
{

    public void GoBackScene1 ()
    {
        Fader.instance.Perform ("Scene1");

    }

    public void Scene2 ()
    {
        Fader.instance.Perform ("Scene2");
    }

    public void Scene3 ()
    {

        Fader.instance.Perform ("Scene3");
    }

    public void Scene4 ()
    {
        Fader.instance.Perform ("Scene4");

    }


}

Scene Fader Script:

public class Fader : MonoBehaviour {

    public static Fader instance;

    [SerializeField]
    private GameObject canvas;

    [SerializeField]
    private Animator anim;


    void Awake(){

        makeSingleton ();
    }


    public void Perform(string levelname)
    {
        StartCoroutine(FadeInAnimation(levelname));


    }

    void makeSingleton(){

        if (instance != null) {

            Destroy (gameObject);
        } else {
            instance = this;
            DontDestroyOnLoad(gameObject);

        }



    }

    IEnumerator FadeInAnimation(string level){

        canvas.SetActive (true);

        anim.Play ("FadeIn");

        yield return new WaitForSeconds (1f);

        Application.LoadLevel (level);

        anim.Play ("FadeOut");

        yield return new WaitForSeconds (2f);

        canvas.SetActive (false);


    }

}

Upvotes: 5

Views: 375

Answers (1)

Yuri Nudelman
Yuri Nudelman

Reputation: 2943

Here is the thing:

Fade in animation length is 0.417 seconds. Between the start of animation and LoadLevel, you wait for 1 second. That means the screen stays black for 0.583 seconds.

The problem is that during all this time, all other buttons are clickable. So if you click "Scene 2", then click the black screen, you can accidentally hit "Scene 3" button, which will initiate FadeInAnimation("Scene3"). Since you made Fader non-destroyable singleton, level load will not stop FadeInAnimation("Scene3") execution.

What you need is some locking, to prevent clicking other buttons while screen is black. For example, something like this will do the job:

private bool fading = false;

IEnumerator FadeInAnimation(string level){

    if (fading) yield break;
    fading = true;

    canvas.SetActive (true);

    anim.Play ("FadeIn");

    yield return new WaitForSeconds (1f);

    Application.LoadLevel (level);

    anim.Play ("FadeOut");

    yield return new WaitForSeconds (2f);

    canvas.SetActive (false);

    fading = false;

}

Upvotes: 1

Related Questions