Switching back and fourth between scenes help (script)
I have been trying to find help on forums without much luck. I currently have 2 scenes.
Scene A loads first and I am able to move to scene B. However I would like to move back to scene A but I can't
This is my scene control script
using System;
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
public class SceneController : MonoBehaviour
{
public event Action BeforeSceneUnload; //Action have no parameters and no return type
public event Action AfterSceneLoad;
public CanvasGroup faderCanvasGroup;
public float fadeDuration = 1f;
public string startingSceneName = "Study";
public string initialStartingPositionName = "DoorToCorridor";
public SaveData playerSaveData;
private bool isFading;
private IEnumerator Start()
{
faderCanvasGroup.alpha = 1f;
playerSaveData.Save(PlayerMovement.startingPositionKey, initialStartingPositionName);
yield return StartCoroutine(LoadSceneAndSetActive(startingSceneName));
StartCoroutine(Fade(0f));
}
public void FadeAndLoadScene(SceneReaction sceneReaction)
{
if (!isFading)
{
StartCoroutine(FadeAndSwitchScenes(sceneReaction.sceneName));
}
}
private IEnumerator FadeAndSwitchScenes(string sceneName)
{
yield return StartCoroutine(Fade(1f));
if (BeforeSceneUnload != null)
{
BeforeSceneUnload();
}
yield return SceneManager.UnloadSceneAsync(SceneManager.GetActiveScene().buildIndex);
yield return StartCoroutine(LoadSceneAndSetActive(sceneName));
if (AfterSceneLoad != null)
{
AfterSceneLoad();
}
yield return StartCoroutine(Fade(0f));
}
private IEnumerator LoadSceneAndSetActive(string sceneName)
{
yield return SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);
Scene newlyLoadedScene = SceneManager.GetSceneAt(SceneManager.sceneCount - 1);
SceneManager.SetActiveScene(newlyLoadedScene);
}
private IEnumerator Fade(float finalAlpha)
{
isFading = true;
faderCanvasGroup.blocksRaycasts = true;
float fadeSpeed = Mathf.Abs(faderCanvasGroup.alpha - finalAlpha) / fadeDuration; //cacualting difference between canvas and final alpha and speed
while (!Mathf.Approximately(faderCanvasGroup.alpha, finalAlpha)) //while not final alpha
{
faderCanvasGroup.alpha = Mathf.MoveTowards(faderCanvasGroup.alpha, finalAlpha, fadeSpeed * Time.deltaTime);
yield return null;
}
isFading = false;
faderCanvasGroup.blocksRaycasts = false;
}
}
I know the issue is with "Scene newlyLoadedScene = SceneManager.GetSceneAt(SceneManager.sceneCount - 1);" how do I change this line of code so I can move back and forth between the scenes.
I have been looking at a script for the 'Adventure Game' tutorial on the unity website and that is where I got the majority of the script from. It works fine in that tutorial scene but not in mine
Thanks in advance
Your answer
![](https://koobas.hobune.stream/wayback/20220612191259im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Fade between scenes 0 Answers
How to move to next scene using scene manager? 3 Answers
LoadScene not working 0 Answers
Why isn't my start screen loading? 0 Answers