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 bundoorahul · Mar 18, 2018 at 07:07 AM · variablelevel

How to make a variable to not reset when a scene changes?

Hello, Ok so this question may sound stupid but i'm new to unity and i'm implementing my second game where the game levels is on different scene and when i complete the first level and move on to the second level, the score should not reset and and should increment on the previous level score...Can anyone explain to me how this can be done please. Thanks

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

2 Replies

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

Answer by danielburnitt · Mar 18, 2018 at 08:29 AM

SohailBukhari is right. Playerprefs is how you would save the variables for each scene. took me a while to understand it myself.

but in my case I have all my variables that i want saved throughout each scene in one script that I can call the save function throughout my code with getters and setters.

 public class GameManager : MonoBehaviour 
 {
 
 //this code lets me call my gamemanager script from other scripts
     private static GameManager instance;
     public static GameManager Instance{get{return instance;}}
 
 //these are my variables that I have i want saved throughout the game
     public int currentSkinIndex = 0;
     public int currency = 0;
     public int skinAvailability = 1;
     public int loadCount = 0;
         public bool disabledAds = false;
 

In the Awake method ,In my code, I have a string set that if my player has the 1st skin there was a previous session and need to check all the variables I list to update their values if they need to be updated. yours can been whatever.

//if player has had a previous session the you list your variables you would like to update next. make sure instead of PlayerPrefs.HasInt you put .GetInt after each variable. then in the () after .GetInt place your string name that you wish to save in your registry.

     private void Awake()
     {
         instance = this;
 
         /////code for determining saves and not saved games////
 
         if (PlayerPrefs.HasKey ("CurrentSkin"))
                { 
             currentSkinIndex = PlayerPrefs.GetInt("CurrentSkin");
             currency = PlayerPrefs.GetInt("Currency");
             skinAvailability = PlayerPrefs.GetInt("SkinAvailability");
 
           //fun fact: these last two are my way of saving how many times my player dies/restarts the game I want between each ad.
             loadCount = PlayerPrefs.GetInt ("Count");
             disabledAds = (PlayerPrefs.GetInt("noAds") != 0);
            } else 
 
 //after you enter all of these you call your save function that is below.
         {
             Save();
         }
     }
     

in your save function you use PlayerPrefs.SetInt to save each variable to your registry. here inside the () you have to put the string name you want in the registry and your variable.

 public void Save()
     {
         PlayerPrefs.SetInt("CurrentSkin", currentSkinIndex);
         PlayerPrefs.SetInt("Currency" , currency);
         PlayerPrefs.SetInt ("SkinAvailability", skinAvailability);
         PlayerPrefs.SetInt ("Count", loadCount);
         PlayerPrefs.SetInt("noAds", (disabledAds ? 1 : 0));
     }

And then whenever I want to call my save function like at the end of a level or when the player dies where ever I am at in the script I just and a simple line like...

 GameManager.instance.save();

And that calls the save function into the registry to save the values of each variable I put in my GameManager.

And thats it. I hope this wasn't too confusing for you and I hope it helps you with your game. Good luck programming!

Comment
Add comment · Show 2 · 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 danielburnitt · Mar 18, 2018 at 08:47 AM 1
Share

I am sorry. I kind of didn't answer your question about saving your score.

how I would do it would be something like

      public class Game$$anonymous$$anager : $$anonymous$$onoBehaviour 
      {
      
          private static Game$$anonymous$$anager instance;
          public static Game$$anonymous$$anager Instance{get{return instance;}}
     
         public int score = 0;
         
         private void Awake()
             {
                 instance = this;
                  if(PlayerPrefs.Haskey("Score"))
                      {
                           score =PlayerPrefs.GetInt("Score");
                      }else
                           {
                                Save();
                           }
              }
         
         public void Save()
                 {
                       PlayerPrefs.SetInt("Score, score);
                  }
     }
avatar image bundoorahul danielburnitt · Mar 18, 2018 at 07:12 PM 0
Share

Thanks man...it worked

avatar image
1

Answer by SohailBukhari · Mar 18, 2018 at 07:18 AM

You should use Playerprefs for score.

See the documentation of PlayerPrefs https://docs.unity3d.com/ScriptReference/PlayerPrefs.html

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 bundoorahul · Mar 18, 2018 at 07:13 PM 0
Share

Thanks for the help

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

82 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

Related Questions

Is there a function for this? 1 Answer

Upload level details, variable always null. 0 Answers

How do I know whether variable is pass by value or by reference? 3 Answers

Move value to 0 in 1.5 seconds 3 Answers

Reading variable from other script 1 Answer


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