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 emek12 · Dec 24, 2013 at 04:05 AM · playerprefssavesaving

Saving Data! PLEASE HELP

Im making an achivements type thing in my game where once you get a certain nnumber of points you complete a challenge and the game saves it. But its not working. Im new t Player.Prefs and what not.

  var challenge = 0;
  var challenge2 = 0;
  var challenge3 = 0;
  var challenge4 = 0;
  var challenge5 = 0;
 
 
 function Start () {
 
     OnSave();
 
 }
 
 
 
 function Update () {
 
     if(CollectGem.score >= 10) //If score is 10 or more Challenge 1 is COMPLETE
     {
     
         challenge = 1;
         OnSave();
         Debug.Log("CH1 IS DONE");
     
     
     }
 }
 
 
 
 
 var help = false;
 var credits = false;
 var challenges = false;
 //var shop = false;
 
 
 function OnGUI()
 {
 
     //GUI.Label(Rect(720,20,300,200),"HINT: RED = The path of the shuriken");
 
     //HELP DISPLAY
     if(help == true)
     {
     
         GUI.Box(Rect(400,210,300,200),"HELP");
         GUI.Label(Rect(420,250,300,200),"GOAL: Collect Gems & Dodge Shuriken");
         GUI.Label(Rect(420,280,300,200),"Gems = +10 points");
         GUI.Label(Rect(420,310,300,200),"Press Space Bar to use power-up");
         GUI.Label(Rect(420,340,300,200),"Hearts give you 1 EXTRA LIFE (MAX)");
         GUI.Label(Rect(420,370,300,200),"Hearts & Power-Ups spawn every 20 secs");
         
         if(GUI.Button(Rect(650,210,50,25),"Close"))
         {
             help = false;
     
         }
     
     }
     
     //CREDITS DISPLAY
     if(credits == true)
     {
     
         GUI.Box(Rect(400,210,300,200),"CREDITS");
         GUI.Label(Rect(420,250,300,200),"Programmer: Emek Umoh");
         GUI.Label(Rect(420,280,300,200),"Graphics Artist: Emek Umoh");
         GUI.Label(Rect(420,310,300,200),"Music: Ryan Little AKA TheR4C2010");
         
         if(GUI.Button(Rect(650,210,50,25),"Close"))
         {
             credits = false;
     
         }
     
     }
     
     //CHALLENGES DISPLAY
     if(challenges == true)
     {
     
         GUI.Box(Rect(400,210,300,200),"CHALLENGES");
         GUI.Label(Rect(420,250,300,200),"Earn 10 points"); //challenge 1
         GUI.Label(Rect(420,280,300,200),"Earn 50 points");
         GUI.Label(Rect(420,310,300,200),"Earn 100 points");
         GUI.Label(Rect(420,340,300,200),"Earn 200 points");
         GUI.Label(Rect(420,370,300,200),"Earn 400 points");
         GUI.Label(Rect(420,400,300,200),"Get a Shuriken");
         
         
         if(challenge == 1)
         {
             GUI.Label(Rect(500,250,300,200),"[COMPLETE]");
             Debug.Log("C1 COMPLETE");
             
         }
         
         
         
         if(GUI.Button(Rect(650,210,50,25),"Close"))
         {
             challenges = false;
     
         }
     
     }
     
 
     
     
 
     if(GUI.Button(Rect(210,150,100,50),"PLAY"))
     {
         Death.spawnWeapon = true;
         Application.LoadLevel("Scene1");
         credits = false;
         help = false;
         challenges = false;
         
     
     }
     
     
     if(GUI.Button(Rect(210,210,100,50),"HELP"))
     {
 
         help = true;
         
         credits = false;
         
         challenges = false;
     
     }
     
     
     if(GUI.Button(Rect(210,270,100,50),"CREDITS"))
     {
     
         credits = true;
         
         
         help = false;
         challenges = false;
         
     
     }
     
     if(GUI.Button(Rect(210,330,100,50),"CHALLENGES"))
     {
     
         credits = false;
         help = false;
         
         challenges = true;
         
     
     }
 
 
 
 
 }
 
 function OnSave()
 {
     PlayerPrefs.SetInt("Challenge",challenge);
     PlayerPrefs.Save();
     
     challenge = PlayerPrefs.GetInt("Challenge");
     print(PlayerPrefs.GetInt("Challenge"));
 
 
 
 }
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 iwaldrop · Jan 15, 2014 at 06:08 AM 0
Share

Did my answer not help you? It's bad form to ask a question and then abandon it.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by iwaldrop · Dec 24, 2013 at 05:56 AM

Your problem is probably due to the fact that you call OnSave in Start with a challenge value equal to 0. That will save 0 to your key which effectively resets it's value each time you start your game.

Also, you have no clear way of determining which challenges were completed since you will be saving all of your challenge info into "Challenge". Probably the simplest approach to keep all of your values in one key is a bitmask. It will allow you to store many flags in one integer.

The following is code that you can drop onto a GameObject and play around with. It's designed so that you can select which challenge to 'complete' by using the enum drop-down in the inspector and then enabling the script. Additionally, you can reset the script (via the gear icon in the upper-right-hand side of the script on the game object) and start over. It's not designed to be used in production, so you'll have to glean what's relevant and refactor your code base to use the parts that you like.

 #pragma strict
 
 private var CHALLENGE_MASK_KEY : String = "CHALLENGE_MASK";
 
 enum Challenge
 {
     ScoreTenPoints = 1,
     BeatUpTwentyBaddies = 2,
     DeflowerTheMaidens = 4,
 }
 
 var whichChallenge : Challenge = Challenge.ScoreTenPoints;
 var challengeFlags : int  = 0;
 
 function Awake()
 {
     LoadChallengeScore();
     enabled = false;
 }
 
 function Reset()
 {
     PlayerPrefs.SetInt(CHALLENGE_MASK_KEY, 0);
 }
 
 function OnEnable()
 {
     CompleteChallenge(whichChallenge);
     enabled = false;
 }
 
 function CompleteChallenge(challenge : Challenge)
 {
     var challengeBit : int = challenge;
     challengeFlags |= challengeBit;
     PlayerPrefs.SetInt(CHALLENGE_SCORE_KEY, challengeFlags);
     print (String.Format("setting flags = {0}", challengeFlags));
 }
 
 function LoadChallengeScore()
 {
     challengeFlags = PlayerPrefs.GetInt(CHALLENGE_MASK_KEY, 1);
     print(challengeFlags);
 }

There are, of course, many ways to solve the problem, but this seems to mate closely with your existing actions.

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

20 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

Related Questions

PlayerPrefs.DeleteAll() not deleting unless app restarts 0 Answers

PlayerPrefs not saving after i quiet application 1 Answer

saving highscores 1 Answer

Adding Value to Already Saved Values 1 Answer

Saving and loading game with PlayerPrefs 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