Having issues carrying variables between scenes when reloading a scene.
I have a script called player profile which looks like this:
public class PlayerProfile : MonoBehaviour
{
public static PlayerProfile instance;
public string playerName, playerTitle;
public int crowns;
public int currentLevel;
public int correctAnswers;
public int incorrectAnswers;
public bool tutorialComplete;
private void Awake()
{
instance = this;
DontDestroyOnLoad(this.gameObject);
}
}
I assign values to these variables when creating a new game, after creating the new game the player is moved to the game scene, the values are all callable and work great, however, after completing a level and going back to the menu screen, the variables do not seem to carry back across and none of the variables is callable anymore.
I am using PlayerProfile.instance.example to call one of the variables.
Is this an issue with how I go back to a scene previously loaded? Do I have to do anything extra when loading a scene that has already been previously loaded?
Hi every time you get back to the scene that created this static instance is regenerate that instance and you lose all data. you need to check if that instance is null or not or its already created or not. you can also use already posted singletons. or do google you knew the problem.
Your answer

Follow this Question
Related Questions
Scene takes too long to load 1 Answer
Scene clones itself and switches continuously. 0 Answers
Fade between scenes 0 Answers
Order of SceneManager.LoadSceneAsync and UnloadSceneAsync. 0 Answers
Event when the scene is loaded 1 Answer