Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by $$anonymous$$ · Aug 24, 2018 at 08:36 AM · playerprefsload scenesave scene

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 :)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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);

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image $$anonymous$$ · Aug 24, 2018 at 12:27 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

91 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges