- Home /
SceneFader
I am extremely frustrated right now! I have done days of research on forums, unity tuts / answers, google, and youtube and have no luck getting a scenefader to work. All of the tutorials go show you how to make scene faders for level progression. My game is a high score game and I have a restart button, home button, and gameover scene currently that I would like to incorporate a fade effect with. I can't believe the simplest things are giving me trouble. I am so close to finishing this game!
I got close with this tutorial (https://www.youtube.com/watch?v=0HwZQt94uHQ), but ran into this issue - http://answers.unity3d.com/questions/1210640/coroutine-issue-1.html
I have tried creating an image and changing the alpha manually when a new scene is loaded (using a bool if (scenechanged == true), but there was no effect.
What can I do?
This is how my scenechanging is setup:
public void Home(){
AreYouSureYouWantToQuit.SetActive (true);
}
public void YESQUIT(){
//SceneFader.instance.LoadLevel("Title Menu");
sceneChanged=true;
SceneManager.LoadScene ("Title Menu");
//StartCoroutine (SceneFaderHome());
//StartCoroutine(FadeToClear());
}
Here's some of the other code I've tried:
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class SceneFader : MonoBehaviour {
/*public Texture2D fadeOutTexture;
public float fadeSpeed = 0.8f;
private int drawDepth = -1000;
private float alpha = 1.0f;
private int fadeDir = -1;
void OnGUI(){
alpha += fadeDir * fadeSpeed * Time.deltaTime;
alpha = Mathf.Clamp01 (alpha);
GUI.color = new Color (GUI.color.r, GUI.color.g, GUI.color.b, alpha);
GUI.depth = drawDepth;
GUI.DrawTexture(new Rect (0,0,Screen.width ,Screen.height),fadeOutTexture);
}
public float BeginFade(int direction){
fadeDir = direction;
return (fadeSpeed);
}
void OnLevelWasLoaded(){
BeginFade (-1);
}*/
public static SceneFader instance;
[SerializeField] private GameObject fadePanel;
[SerializeField] private Animator fadeAnim;
// Use this for initialization
void Awake () {
MakeSingleton ();
}
// Update is called once per frame
void Update () {
}
void MakeSingleton(){
if (instance != null) {
Destroy (gameObject);
} else {
instance = this;
DontDestroyOnLoad (gameObject);
}
}
public void LoadLevel (string level){
StartCoroutine (FadeInOut (level));
}
IEnumerator FadeInOut(string level){
fadePanel.SetActive(true);
fadeAnim.Play("SceneFadeIn Animation");
yield return new WaitForSeconds (1f);
SceneManager.LoadScene(level);
fadeAnim.Play("SceneFadeOut Animation");
yield return new WaitForSeconds (0.7f);
fadePanel.SetActive(false);
}
}
This image goes with the above code (that is not commented out) and again led to the null reference error??
[1]: /storage/temp/73340-screen-shot-2016-07-02-at-112421-am.png
I would really appreciate any help.
Answer by aditya · Jul 02, 2016 at 01:27 PM
simply create a UI panel with a image of color you want your scene to fade in and then use the below script ... DO NOT JUST COPY PASTE THE WHOLE CODE, WRITTEN WITHOUT EDITOR IN FRONT
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class fadeScene : Monobehaviour{
public Image panelObject;
void YesQuit(){
panelObject.CrossFadeAlpha(1f, 0.2f, true);
Invoke("loadNextScene", 0.3f);
}
void loadNextScene(){
SceneManger.LoadScene("Title Menu");
}
}
Learn more about CrossFadeAlpha Here
Thank for getting back to me! I tried what you said, but the fade doesn't work and the scene does not change. The scene changes once Resume is pressed, but no fade. Here's my updated code:
using UnityEngine;
using System.Collections;
using UnityEngine.Scene$$anonymous$$anagement;
using UnityEngine.UI;
public class PausePanel : $$anonymous$$onoBehaviour {
//public GUITexture overlay;
//public float fadeTime;
public Slider audioSlider;
public Slider sensitivitySlider;
private Player instance;
public GameObject AreYouSureYouWantToQuit;
public GameObject AreYouSureYouWantToRestart;
public GameObject HowToPlayPanel;
public GameObject SettingsPanel;
public static bool isPaused=false;
public Text highScoreText;
public Text goldTeethText;
public GameObject pause$$anonymous$$enuCanvas;
public Image panelObject;
//private bool home = false;
//private bool restart = false;
// Update is called once per frame
void Update () {
if (isPaused == true) {
pause$$anonymous$$enuCanvas.SetActive (true);
Time.timeScale = 0f;
} else if (isPaused==false && TimeChangingPowerUp.Active == true) {
pause$$anonymous$$enuCanvas.SetActive(false);
Time.timeScale = 0.5f;
}
else {
pause$$anonymous$$enuCanvas.SetActive(false);
Time.timeScale = 1f;
}
}
public void Resume(){
isPaused = false;
SettingsPanel.SetActive (false);
HowToPlayPanel.SetActive (false);
AreYouSureYouWantToRestart.SetActive (false);
AreYouSureYouWantToQuit.SetActive (false);
}
public void Restart(){
AreYouSureYouWantToRestart.SetActive (true);
}
public void YESRESTART(){
//restart = true;
panelObject.CrossFadeAlpha (1f, 0.2f, true);
Invoke ("loadNextScene", 0.3f);
//Scene$$anonymous$$anager.LoadScene ("Gameplay");
//SceneFader.instance.LoadLevel("Gameplay");
//StartCoroutine (SceneFaderGamePlay());
}
public void NEVERRESTART(){
AreYouSureYouWantToRestart.SetActive (false);
}
public void Home(){
AreYouSureYouWantToQuit.SetActive (true);
}
public void YESQUIT(){
//home = true;
panelObject.CrossFadeAlpha (1f, 0.2f, true);
Invoke ("loadNextScene", 0.3f);
//SceneFader.instance.LoadLevel("Title $$anonymous$$enu");
//Scene$$anonymous$$anager.LoadScene ("Title $$anonymous$$enu");
//StartCoroutine (SceneFaderHome());
//StartCoroutine(FadeToClear());
}
void loadNextScene(){
Scene$$anonymous$$anager.LoadScene ("Title $$anonymous$$enu");
/*if (restart==true) {
Scene$$anonymous$$anager.LoadScene ("Gameplay");
}*/
}
i see nothing wrong with this updated code if your both YESRESTART
and YESQUIT
getting called, in this code i can't see these methods being called anywhere so i m assu$$anonymous$$g that your calling them either from UI buttons or some external script .... use debugs in these functions and make sure that they are getting called or not ... it is strange if this script is not working because you have just two lines here, one that is fading the panel's alpha and other that is loading the scene ... and make sure to keep the alpha of fader panel to zero on scene start
Your answer
Follow this Question
Related Questions
Fadeout Issue 0 Answers
Moving to next scene takes large time to load 1 Answer
Load 2 scenes at once throuh scipt 0 Answers
alpha value not changing(gradual decrease) during runtime 1 Answer