Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Scene change / loading help

Discussion in 'Editor & General Support' started by CrazyCGChick85, Jan 29, 2019.

  1. CrazyCGChick85

    CrazyCGChick85

    Joined:
    Aug 30, 2018
    Posts:
    19
    Hi I hope someone can shed some light on an issue I am having.

    I have set up a scene change from scene A to scene B which works fine. However I would like to move from scene B to scene A but for some reason it is not working. See video below (Please ignore the camera error, that's a different issue I am trying to fix at the same time)



    This is my scene controller script
    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using UnityEngine.SceneManagement;
    4. using System.Collections;
    5.  
    6. public class SceneController : MonoBehaviour
    7. {
    8.  
    9.     public event Action BeforeSceneUnload;
    10.     public event Action SceneAfterLoad;
    11.  
    12.     public CanvasGroup faderCanvasGroup;
    13.     public float fadeDuration = 1;
    14.     public string StartingSceneName = "Study";
    15.     public string intialStartingPosition = "DoorToCorridor";
    16.     public SaveData playerSaveData;
    17.  
    18.     private bool isFading;
    19.  
    20.     private IEnumerator Start()
    21.     {
    22.         faderCanvasGroup.alpha = 1f;
    23.  
    24.         playerSaveData.Save(PlayerMovement.startingPositionKey, intialStartingPosition);
    25.  
    26.         yield return
    27.             StartCoroutine(LoadSceneAndSetActive(StartingSceneName));
    28.  
    29.         StartCoroutine(Fade(0f));
    30.     }
    31.  
    32.     public void FadeAndLoadScene(SceneReaction sceneReaction)
    33.     {
    34.         if (!isFading)
    35.         {
    36.             StartCoroutine(FadeAndSwitchScenes(sceneReaction.sceneName));
    37.         }
    38.         Debug.Log("fade");
    39.  
    40.     }
    41.  
    42.     private IEnumerator FadeAndSwitchScenes(string sceneName)
    43.     {
    44.        
    45.         yield return StartCoroutine(Fade(1f));
    46.  
    47.         if (BeforeSceneUnload != null)
    48.         {
    49.             BeforeSceneUnload();
    50.         }
    51.        
    52.         yield return SceneManager.UnloadSceneAsync(SceneManager.GetActiveScene().buildIndex);
    53.  
    54.         yield return StartCoroutine(LoadSceneAndSetActive(sceneName));
    55.  
    56.         if (SceneAfterLoad != null)
    57.         {
    58.             SceneAfterLoad();
    59.         }
    60.         yield return StartCoroutine(Fade(0f));
    61.  
    62.         Debug.Log("Fade");
    63.      
    64.     }
    65.  
    66.     private IEnumerator LoadSceneAndSetActive(string sceneName)
    67.     {
    68.         yield return SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);
    69.  
    70.         Scene newlyLoadedScene = SceneManager.GetSceneAt(SceneManager.sceneCount - 1);
    71.  
    72.         SceneManager.SetActiveScene(newlyLoadedScene);
    73.  
    74.         Debug.Log("Scene Loaded");
    75.        
    76.     }
    77.  
    78.     private IEnumerator Fade (float finalAlpha)
    79.     {
    80.         isFading = true;
    81.         faderCanvasGroup.blocksRaycasts = true;
    82.  
    83.         float fadeSpeed = Mathf.Abs(faderCanvasGroup.alpha - finalAlpha) / fadeDuration; //Calculates the difference between canvas and final fade speed
    84.  
    85.         while
    86.             (!Mathf.Approximately(faderCanvasGroup.alpha, finalAlpha))
    87.         {
    88.             faderCanvasGroup.alpha = Mathf.MoveTowards(faderCanvasGroup.alpha, finalAlpha, fadeSpeed * Time.deltaTime);
    89.             yield return null;
    90.         }
    91.  
    92.         isFading = false;
    93.         faderCanvasGroup.blocksRaycasts = false;
    94.  
    95.         Debug.Log("Active scene");
    96.     }
    97.  
    98. }
    99.  
    Thanks in advanced
     
  2. pitaya4

    pitaya4

    Joined:
    Mar 25, 2017
    Posts:
    8
    I am a beginner too and this may be completely wrong, but that's my suspicion.

    Code (CSharp):
    1. Scene newlyLoadedScene = SceneManager.GetSceneAt(SceneManager.sceneCount - 1);
    What you're essentially doing is getting the total count of scenes (that you added to the Build window), get the pre-last one and load it.

    So, let's say you have 3 scenes:
    1: Main Menu
    2: Room A
    3: Room B

    If you're in room B and try to execute the above-mentioned line of code it will take you to Room A, because 3-1=2 and scene #2 is Room A.
    However, if you're in Room A, you will try to go to Room A, hence why it doesn't work.


    (also your character is dope)
     
  3. CrazyCGChick85

    CrazyCGChick85

    Joined:
    Aug 30, 2018
    Posts:
    19

    Thanks for your help. I thought it might have something to do with that line of code but it is based on a script from a tutorial (Adventure Game) with the exact same line of code and there were no issues in that tutorial file. It must be something in the scene build that I am missing.

    Thanks about the character but this is my test guy while I design my actual character. I actually got him from the asset store
     
    Last edited: Feb 5, 2019