- Home /
Why is OnLevelWasLoaded () called twice and why are my variable's values different in each call?,How do I use a variable right after scene change?
I currently have 2 scenes, house scene and overworld scene. Whenever the player enters the house scene, I will save the previous scene with currentArea and position with currentPosition in that scene the player was at. After exiting the house scene, the player will go back to where they last were by checking if the loaded scene's name is currentArea. However, I am currently encountering a problem as I notice that OnLevelWasLoaded() is called twice when a scene is loaded. Furthermore, currentArea will contain the previous scene's data in one call but be empty in another call. Is there a way to prevent this from occurring. Below is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneTransition : MonoBehaviour
{
public static SceneTransition instance = null;
[SerializeField] string currentArea = "";
[SerializeField] Vector3 currentPosition;
void Awake()
{
if (instance == null)
instance = this;
else if (instance != this)
{
Destroy(gameObject);
return;
}
DontDestroyOnLoad(gameObject);
if (SceneManager.GetActiveScene().name == currentArea)
{
BasePlayer.instance.transform.position = currentPosition;
CameraController.instance.transform.position = new Vector3(currentPosition.x, currentPosition.y, CameraController.instance.transform.position.z);
}
else if (GameObject.FindGameObjectWithTag("StartingPoint"))
{
BasePlayer.instance.transform.position = GameObject.FindGameObjectWithTag("StartingPoint").transform.position;
CameraController.instance.transform.position = new Vector3(GameObject.FindGameObjectWithTag("StartingPoint").transform.position.x, GameObject.FindGameObjectWithTag("StartingPoint").transform.position.y, CameraController.instance.transform.position.z);
}
}
private void OnLevelWasLoaded(int level)
{
Debug.Log("currentArea = " + currentArea);
if (SceneManager.GetActiveScene().name == currentArea)
{
BasePlayer.instance.transform.position = currentPosition;
CameraController.instance.transform.position = new Vector3(currentPosition.x, currentPosition.y, CameraController.instance.transform.position.z);
}
else if (GameObject.FindGameObjectWithTag("StartingPoint"))
{
BasePlayer.instance.transform.position = GameObject.FindGameObjectWithTag("StartingPoint").transform.position;
CameraController.instance.transform.position = new Vector3(GameObject.FindGameObjectWithTag("StartingPoint").transform.position.x, GameObject.FindGameObjectWithTag("StartingPoint").transform.position.y, CameraController.instance.transform.position.z);
}
}
public void LoadArea(string nextArea)
{
SceneManager.LoadScene(nextArea);
}
public void LoadEvent(string nextScene)
{
SaveCurrentArea();
SceneManager.LoadScene(nextScene);
}
public void SaveCurrentArea()
{
currentArea = SceneManager.GetActiveScene().name;
currentPosition = BasePlayer.instance.transform.position;
}
public void LoadCurrentArea()
{
SceneManager.LoadScene(currentArea);
}
}
Answer by Captain_Pineapple · Aug 24, 2019 at 10:12 AM
Hey there,
have you tried adding the "singleton-check" that you do in your awake function to your OnLevelWasLoaded callback? even though this should not be the problem it wont hurt to check.
Your answer
Follow this Question
Related Questions
My scene won't load after building the project but i can hear the audio 0 Answers
Enter building script for a open world game 0 Answers
Rotation stops when reload scene 1 Answer
How to prevent a script from running until the scene is loaded? 2 Answers
Can you create transitions between levels using Application.LoadLevel? 6 Answers