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 JasonHong0620 · May 25, 2019 at 07:07 AM · playerprefsreloadlives

How to save player's lives using playerprefs while I load the same scene?

I made a 2D game in unity.

And I set a trap when the player has a collision on it, it will dead and can't move.

Meanwhile,I load the same scene to make my character reborn and reset the objects.

I set the key "Tab" to use SceneManager.loadscene.

But I don't know where to set the text about number of lives.

I use PlayerPrefs to set and get int,but it seems that the number will be reseted when the scene reload.

When I start the game and make the player die,while I press Tab,it becomes 2 but turn back to 3 in a second.

Is it because the scene reload?

Script 1:

 public class MainMenu : MonoBehaviour
 {
 public int currentlive;

     public void NewGame()
     {
     PlayerPrefs.SetInt("currentLives", currentlive);
     }
 }

Script 2:

 void Start()
 {
     life = PlayerPrefs.GetInt("currentLives");
 }
 void OnCollisionEnter2D(Collision2D collision)
 {
     if (collision.gameObject.tag=="Trap")
         {
             object1.SetActive(true); //Show Trap

             run = false;
             jump = false;
             dead = true;  //animations

             lifecounter--;  //lives decrease
             PlayerPrefs.SetInt("currentLives", lifecounter);
         }
 }

Script 3:

 void Update();
 {
     if (dead == true)  //start the dead animation
         {
             player.GetComponent<Player>().enabled = false;  //To make the player can't move
             if (Input.GetKeyDown(KeyCode.Tab))
             {
                 EditorSceneManager.LoadScene(EditorSceneManager.GetActiveScene().name); //reload scene
                 Life.text = "X " + PlayerPrefs.GetInt("currentlife"); //show the lives on the game
             }
         }
 }
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 mchts · May 25, 2019 at 07:44 AM 0
Share

Guess your lifecounter is a hardcoded value(for e.g. 3) so it will be same as new scene loaded. If this is the problem you should do sth. like below in 2nd script:

 void Start()
  {
      life = PlayerPrefs.GetInt("currentLives");
      lifecounter = life;
  }

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by ItsKhanny · May 25, 2019 at 08:01 AM

I understand what you are trying to do, but you can do it in more efficient ways. There is one way that comes to mind


Create a game object that has a game manager script on it. It'll be used to save player lives and such. Create an Awake method with the line of code

 DontDestroyOnLoad(this.GameObject);

This is used to keep the game object from being deleted between scenes. So any variables saved won't be restarted. You then can destroy the game object when going to the menu using Destroy(game manager gameobject) inserting what ever you need to destroy it. The last part you will need to know is to prevent the gameobject from being set a 2nd time. Best option is to find the gameobject using GameObject.Find(gameobject name) and checking to see if one exists, if it doesn't, then spawn it in.


Setting the text to show lives can be easy too. If you are looking for a fast an easy way, just create a Text variable to put the UI gameobject in, then in the update method, put the following

 livesTxt.text = "Lives: " + lives;

Or something similar to that. Best option is to put it in the gamemanager script too so you don't have to call it

If you still don't understand, I can create an example script for you to show you how to get the code to work

Comment
Add comment · 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
0

Answer by UltraGearGames · May 26, 2019 at 06:46 PM

But why playerprefs? Just use a static variable.

Comment
Add comment · 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

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

114 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

Related Questions

UI Button's Images Resets after Scene Reloads, Script Attached 0 Answers

How do i make a script that re-fills lives after 10min? 2 Answers

Instantiate a prefab just one time 1 Answer

PlayerPrefs, First time applying and need help. 2 Answers

Setting intial value for playerprefs 2 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