- Home /
problem changing scene when using playerPrefs. help.
so what I want to do is when user first opens the app, scene1 is loaded, where he is given choices between what type of scene he would prefer, when he hits that a button, that particular scene is loaded, and when next time when he opens the app, app would remember what scene he chose and respective scene is loaded without asking for preferences again.
I used following script
int SceneNumber;
private void Awake()
{
DontDestroyOnLoad(gameObject); //so it remains active between scenes
}
private void Start()
{
LoadScene();
}
private void Update()
{
SceneNumber = SceneManager.GetActiveScene().buildIndex;
}
public void SaveScene()
{
PlayerPrefs.SetInt("SceneLoad", SceneNumber);
}
public void LoadScene()
{
int SceneNumber = PlayerPrefs.GetInt("SceneLoad");
SceneManager.LoadScene(SceneNumber);
}
public void Scene2()
{
SceneManager.LoadScene("Scene2");
SaveScene();
}
public void Scene3()
{
SceneManager.LoadScene("Scene3");
SaveScene();
}
notice how saveScene function is called right when user hits button to go to that scene.
also notice loadScene function is called on Start, so next time user opens app his preferred scene is loaded immediately.
Now here's a problem,
when I hit change scene button, it should take user to scene1 where he again is given choices to which scene he want,
but when I hit ChangeScene button,
scene does change for split second but changes back to his preferred scene because loadScene function is called on start.
How do I change scenes now ?
How should I call LoadScene function such that it is called only when user opens the app and not when he want to change scene ?
thank you in advanced :)
Answer by gvergidis · Aug 24, 2018 at 09:10 AM
Hello.
I do not undrestand very well what you want to say but I will try to catch you up. So, if I undrestood correct, if the user has selected a prefered scene, you do not want onStart to load the first scene. Is that correct? If yes, try setting an if stsatement to onStart method like so :
if (PlayerPrefs.GetInt("SceneLoad") == 0)
{
LoadScene();
}
So, if a user has already picked a scene before, then this int would be different from 0. So, load scene. I hope this helps. Tell me if that did the job.
Also, please, do not use the update method to grab scene number. There is no point for that. Remove the update method and instead, when you want to save the scene, save it like so :
PlayerPrefs.SetInt("SceneLoad", SceneManager.GetActiveScene().buildIndex);
hi, thanks for the reply. yes my english is terrible, I'm from non english speaking country, sorry.
let me explain again, with the script I mentioned above, it does load the preferred scene when user opens the app for second time.
but I'm unable to go to 1st scene where user is given choice to choose from different scenes.
when I press button to go to 1st scene, it automatically opens preferred scene right after.
I think it is because I put LoadScene function on start, so when new scene is loaded, it calls the start function again which calls LoadScene function.
so I want a different way to call LoadScene function.
I want to call LoadScene only when user opens app and not when user is trying to change scene and new scene is loaded.
and yeah removing update method was a good idea, you are very clever.
I just downloaded unity 2 months ago, so I have a lot to learn :)
thanks again.
Your answer
Follow this Question
Related Questions
Can someone tell me what is wrong with this? 2 Answers
How to load player last scene visited 1 Answer
Issues with Calling From Main Thread 2 Answers
How to load a scene with playerprefs from a ui element 1 Answer
(stuck :() How can I save the player position/data when flicking between scenes 0 Answers