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
1
Question by rahulpatil6220375 · Jul 08, 2019 at 02:47 PM · playerprefsintget

Store Current Level Using Preferances?

i m working on 2D game:

problem is here :

when player first level complete then going second level,player out (second level)then load first level not second level

Code:

  void OnTriggerEnter2D(Collider2D other)
     {
         Debug.Log("triger fire");
 
         if (other.gameObject.tag == "Player")                              
         {
             //Debug.Log("findgameobjecct:" + other.gameObject.tag);
             gameovertext.SetActive(true);
             PlayerManager.playerstop = true;
             PlayerManager.obstaclestop = true;
             StartCoroutine(gameover());
         }
     }
 
     private IEnumerator gameover()
     {
         yield return new WaitForSeconds(3f);
 
          //problem:in player second level and player out then load first level not second level
         int num = PlayerPrefs.GetInt("Level",0);
         SceneManager.LoadScene(num);
 
      
         Debug.Log("InsideGameOver");
     }
Comment
Add comment · Show 1
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 aoun111 · Jul 08, 2019 at 05:34 PM 0
Share

If you are getting an int in playerprefs, remove the 0 and the comma at line 21. If you wsnt to set an int, then change it to PlayerPrefs.SetInt....

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Ak0rn · Jul 09, 2019 at 08:18 AM

Your issue is that your PlayerPref isn't updating the int of "Level" in each scene, your script is currently set to have "Level" as 0 so it will always load "0" in the build index.

     Scene currentScene = SceneManager.GetActiveScene();
     int num = PlayerPrefs.SetInt("Level", currentScene.buildIndex + 1);
     SceneManager.LoadScene(num);

That should give you somewhere to start if it doesn't work straight away, can't currently test it.

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 Ak0rn · Jul 09, 2019 at 08:24 AM 0
Share

If you want the correct level to reload when you die, simply put in a Scene$$anonymous$$anager.LoadScene(currentScene) inside of "GameOver"

The above code will update the current level at the end of each level and then load it. Also very useful.

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

115 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 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

Get score from list? 1 Answer

problem with playerprefs 1 Answer

PlayerPrefs setting to an existing int 0 Answers

what's the result of PlayerPrefs.Get... if the key/value was never set? 1 Answer

Set int to object from list? 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