Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 yusufalp727 · Jul 25, 2021 at 12:17 PM · playerprefssaveloadgameovercontinue

How can I continue the game without losing values after Game Over?

Hello How can I continue the game without losing values after Game Over? Let's say Game Over happened and my Player object disappeared from the scene and the player will be able to continue the game by watching an ad. How can I continue the game without losing values ​​such as gold and score? I save my values with playerPrefs. I write this but all values are zero. I guess these games also have an update because new scene has been uploaded.

  public void watchRewardedAdResumeGame(object sender, Reward args)
      {
          SceneManager.LoadScene("Level");
 
          PlayerPrefs.SetInt("Gold",PlayerPrefs.GetInt("Gold") + goldResume);
          goldResume=PlayerPrefs.GetInt("Gold");           
          goldText.text="Gold: " + goldResume;
 
          PlayerPrefs.SetFloat("score",PlayerPrefs.GetFloat("score") + scoreResume);
          scoreResume=PlayerPrefs.GetFloat("score");
          scoreText.text=((int)scoreResume).ToString();
      }
Comment
Add comment · Show 4
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 Anonymous620 · Jul 25, 2021 at 10:04 PM 0
Share

Make a new script and delete the monobehaviour component. Then make all your variables static so that you can access them from other scripts. Then when game over you update the variables on the non MonoBehaviour script and when you load the scene again you load the variables onto the score script. It's basically a save and load script

avatar image logicandchaos Anonymous620 · Jul 26, 2021 at 06:10 PM 0
Share

would make more sense to make make the class static instead of all the variables.. but a scriptable object can do all that and persists between scenes.

avatar image Anonymous620 logicandchaos · Jul 26, 2021 at 09:40 PM 0
Share

Yeah good point

avatar image halbenhb1 · Jul 26, 2021 at 10:26 AM 0
Share

There might be two problems: 1- You first load the scene, then set playerprefs. So your values may be reset. 2-If there is no other script that changes PlayerPrefs.GetInt("Gold"), this script only makes it zero because you did not define goldResume and scoreResume before.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by SoulBlaze06 · Jul 28, 2021 at 09:51 AM

Are you using a different scene for GameOver. If so, then you should not use it and instead you should create a UI Canvas and then an empty gameobject named "game over" and under that gameobject you can parent all your buttons, images and gameover text etc. After that you can simply enable and disable the gameobject so that it only appears when player has lost or simply gameover. In this way you will not have to switch scenes and will also save you time.

public GameObject gameOverMenu; // Creating a gameObject private bool isGameOver; if(isGameOver = true) gameOverMenu.SetActive(true); // if you want to enable the object Time.timeScale = 0; // Just so that your game gets paused and make sure to make it 1 after you want your gameOverMenu To be disabled

else if(isGameOver = false) gameOverMenu.SetActive(false); // if you want to disable the object Time.timeScale = 1; // basically resuming the game so that it is no longer a time freeze

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

136 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Playerprefs When the value I set is zero, there is a 2x increase in the part I get. when i close and reopen the game it only shows the last value. How can ı solve these? 1 Answer

Saving character selection 1 Answer

PlayerPrefs for FPS? 1 Answer

Save/Load Variable with playerprefs 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