Search Unity

My scene don't load immediately

Discussion in 'Scripting' started by DragOOneD999, May 30, 2019.

  1. DragOOneD999

    DragOOneD999

    Joined:
    Jan 10, 2017
    Posts:
    64
    I have a fade out fade in script which work fine in other games. It was working fine even in the game I currently working but for unknown reasons it started to fade out slowly out of the current scene without any black screen.
    here is my fade out/fade in script but I don't think this is the issue
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.SceneManagement;
    6.  
    7. public class SceneFader : MonoBehaviour {
    8.  
    9.     public Image img;
    10.     public AnimationCurve curve;
    11.     private float _progress = 0f;
    12.     public Text loadingText;
    13.  
    14.     private void Start()
    15.     {
    16.         StartCoroutine(FadeIn());
    17.     }
    18.  
    19.     public void FadeTo(string scene)
    20.     {
    21.         StartCoroutine(FadeOut(scene));
    22.     }
    23.  
    24.     IEnumerator FadeIn()
    25.     {
    26.         float t = 1f;
    27.  
    28.         while(t > 0f)
    29.         {
    30.             t -= Time.deltaTime;
    31.             float a = curve.Evaluate(t);
    32.             img.color = new Color(0f, 0f, 0f, a);
    33.             yield return 0;
    34.         }
    35.     }
    36.  
    37.     IEnumerator FadeOut(string scene)
    38.     {
    39.         AsyncOperation operation = SceneManager.LoadSceneAsync(scene);
    40.  
    41.         operation.allowSceneActivation = false;
    42.  
    43.         while(_progress < 1f)
    44.         {
    45.             _progress = Mathf.Clamp01(operation.progress / 0.9f);
    46.  
    47.             loadingText.text = "Loading... " + (int)(_progress * 100f) + "%";
    48.  
    49.             //Debug.Log("Loading... " + (int)(_progress * 100f) + "%");
    50.  
    51.             yield return null;
    52.         }
    53.  
    54.         float t = 0f;
    55.  
    56.         while (t < 1f)
    57.         {
    58.             t += Time.deltaTime;
    59.             float a = curve.Evaluate(t);
    60.             img.color = new Color(0f, 0f, 0f, a);
    61.             loadingText.color = new Color(1f, 1f, 1f, a);
    62.             yield return 0;
    63.         }
    64.  
    65.         //SceneManager.LoadScene(scene);
    66.         operation.allowSceneActivation = true;
    67.     }
    68. }
    69.  
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,195
    Hard to say, as you haven't provided much to go on, and you haven't really described what isn't working. You're saying that you never see the black overlay image "img" ? Or is it always solid black? You could double check whether the sorting layer on the canvas is correct. I've messed up my fade overlay before by having another canvas on top of it, so it doesn't block out anything on that canvas.
     
  3. DragOOneD999

    DragOOneD999

    Joined:
    Jan 10, 2017
    Posts:
    64
    it never show the black image. my sorting layer is set to 0 on fade canvas
     
  4. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,195
    Well, when you think the black image should be showing, pause Play mode, go to the scene view, and see where that image is. See if it's the right color, see if it's maybe in the wrong place, or if something has disabled it.
     
  5. DragOOneD999

    DragOOneD999

    Joined:
    Jan 10, 2017
    Posts:
    64
    I think the problem is with loading the scene
     
  6. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,195
    Well, you haven't explained anything about the loading scene, so I don't know how to help you. Maybe you can provide more details, otherwise it sounds like you're just working through this on your own.

    You've got some Debug.Log statements commented out in the code you have here. Uncomment those. Are they being called?
     
  7. DragOOneD999

    DragOOneD999

    Joined:
    Jan 10, 2017
    Posts:
    64
    the debug say 100% loaded but the scene is still fading out
     
  8. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,195
    Oh. Maybe it's a rounding error. You might never be getting out of your "while(_progress < 1f)" loop, because it's possible that _progress only gets to something like .9999999.

    Check whether you're ever getting out of the loop. If not, then make the loop dependent on operation.progress < 0.9 instead, in case it's the clamp call rounding or something.
     
  9. DragOOneD999

    DragOOneD999

    Joined:
    Jan 10, 2017
    Posts:
    64
    if I comment this lines
    Code (CSharp):
    1. while (t < 1f)
    2.         {
    3.             t += Time.deltaTime;
    4.             float a = curve.Evaluate(t);
    5.             img.color = new Color(0f, 0f, 0f, a);
    6.             loadingText.color = new Color(1f, 1f, 1f, a);
    7.             yield return null;
    8.         }
    the scene load immediately
     
  10. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,195
    Ah. Maybe you're setting Time.timeScale to 0 when you start loading? If so, Time.deltaTime will be 0 each time. You can use unscaledDeltaTime instead if the game is paused.
     
  11. DragOOneD999

    DragOOneD999

    Joined:
    Jan 10, 2017
    Posts:
    64
    that fixed the problem. thank you
     
    dgoyette likes this.