Search code examples
c#unity-game-enginefadingunity-ui

Screen Fades between scenes but the black screen remains when returning to Previous Scene


I'm sorry if the title and my explanation feel a bit off, English is not my native language. So I am making a 2d game for android and I have Several scenes like MainMenu, Level Selection, etc etc. I watched Brackeys transition video and did exactly as he said. The Scene fades from one scene to another as expected but the problem starts when I try to get back to any previous screen form where I have faded out. The screen remains black, that is the alpha channel remains at 1.

This is the Script, I use the FadeTo Function in here for the transition. I Have 2 Animation files 'Start' 'End' in End File the screen goes from transparent [alpha = 0] to Solid black [Alpha = 1]

after winning a level the player has a choice to either go to the main menu or go the level selector scene to select a newly unlocked level but as I said before.. once the screen has been faded out from, it stays Solid black if we try to return to it.

using System.Collections; 
using System.Collections.Generic;
using UnityEditorInternal;
using UnityEngine;
using UnityEngine.SceneManagement;
    
     public class FadeAnimationSC : MonoBehaviour
     {
         public Animator fadeTransition;
         public float transitionTime = 1f;
     
         public void FadeTo(string scene)
         {
             StartCoroutine(fadeAnim(scene));
         }
     
         IEnumerator fadeAnim(string scene)
         {
             fadeTransition.SetTrigger("Start");
             yield return new WaitForSeconds(transitionTime);
             SceneManager.LoadScene(scene);
         }
         
     }

I'd be happy if someone can help me or pin-point me towards a tutorial or an article about the functionality I want. I'd gladly add in additional info if you guys require it. Thanks for your time, I appreciate it!


Solution

  • OK so as you created a fade out at end of the level, you can create a script to fade in when the level has begin.

    using System.Collections; 
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.SceneManagement;
    
     public class FadeInAnimationSC : MonoBehaviour
     {
         // here create another clip and rather than change the alpha 0->1, change it to 1->0 in the animation clip
         public Animator fadeTransition; 
    
         public void Awake()
         {
             fadeTransition.SetTrigger("Start");
         }
         
     }
    

    so this script will fade in at the beginning of the level as it runs on Awake function time, just attach it to the black screen or the object you are using, also if you want to adjust the time to start fading you can using coroutines as in your script. add this script to any level you have the problem with.

    PS: there are solutions better than this, like we can have one script for all this but I can't implement it as I don't see the whole picture of your task.