using UnityEngine;
using System.Collections;

public class ScreenFader : MonoBehaviour {

    public static ScreenFader instance;

    public bool loading = false;
    public string levelToLoad;
    private AudioSource audio;

    void Start() {

        if (instance != null && instance != this) {
            Destroy(this.gameObject);
            return;
        }

        audio = GetComponent<AudioSource>();
        instance = this;
        loading = false;
        DontDestroyOnLoad(transform.gameObject);
        audio.Play();
    }

    public void changeLevel(string level) {
            loading = true;
            levelToLoad = level;
    }

    void Update() {
        if (loading) {
            audio.Pause();
            Application.LoadLevel(levelToLoad);
            loading = false;
            audio.UnPause();
        }

    }


}
