- Home /
 
Fading Out UI On Play
Hi. I have a play button in my game. When the user taps the play button I want all of the UI to fade out. I have a canvas group that controls that alpha of all of these UI elements. Now all I want to do is when the user taps the play button I want all UI elements to fade out and then switch to the game scene. The game scene looks exactly the same except with none of the previous UI. How can I do this?
Answer by fafase · Dec 20, 2014 at 05:22 PM
 [SerializeField] private CanvasGroup canvasGroup = null;
 [SerializeField] private float speed = 1f;
 private bool callOnce = false;
 
 public void LoadNextScene()
 {
     if(callOnce == false){
        StartCoroutine(LoadNextSceneCoroutine());
     }
 }
 private IEnumerator LoadNextSceneCoroutine()    {
     callOnce = true;
     while(canvasGroup.alpha > 0){
         canvasGroup.alpha -= Time.deltaTime * speed;
         yield return null;
     }
     Application.LoadLevel("Scene2");
 }
 
               Attach that to a game object and then drag it into the onClick section of the button. Choose the LoadNextScene method and I guess that should do it.
@fafase I keep getting 2 errors. Here they are:
Assets/Scripts/GoToGameOnPlay.cs(14,25): error CS1502: The best overloaded method match for
UnityEngine.$$anonymous$$onoBehaviour.StartCoroutine(System.Collections.IEnumerator)' has some invalid arguments 2. Assets/Scripts/GoToGameOnPlay.cs(14,25): error CS1503: Argument#1' cannot convertmethod group' expression to typeSystem.Collections.IEnumerator'
I also assume there is suppose to be a space between IEnumerator and LoadNextSceneCoroutine() on line 12.
Why do I keep getting these errors?
Yep my bad I fixed those. U need the space and the parenthesis.
@fafase There is a little pause in the transition before it goes to the game scene. Is there anything that can fix this?
The pause is probably the time it takes I actually load the next scene.
Your answer
 
             Follow this Question
Related Questions
Is there a better way to access the single active Toggle in a ToggleGroup? 4 Answers
A node in a childnode? 1 Answer
Maximize On Play UI is small???? 2 Answers
Unity 4.6B UI Scrollbar Usage 1 Answer
Floating Enemy Health Bars? 1 Answer