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 Mirakulous · Jul 31, 2014 at 08:08 AM · c#playerprefs

player prefs issues

ok so after getting a fix for saving playerprefs using OnApplicationQuit(). i was able to get this to work once. when player runs of lives however and data is cleared to allow lives to be reset, i am met with 0 lives so game will not run. in unity runtime i am met with same issue and it no longer lets me play.

i'll paste code below.

LivesManager

public class LivesManagerScript : MonoBehaviour {

 public static int lives = 6;
 
 void Awake()
 {
     DontDestroyOnLoad (gameObject);
     PlayerPrefs.GetInt ("Lives",lives);
 }
 
 void Update()
 {
     lives = PlayerPrefs.GetInt ("Lives");
 }

 void OnApplicationQuit() 
 {
     PlayerPrefs.SetInt("Lives",CharacterMove.lives);
 }

}

Start Button

public class StartButtonScript : MonoBehaviour {

 public int lives = 0;
 public int dJumps = 0;
 
 void Start()
 {
     lives = PlayerPrefs.GetInt ("Lives");
     dJumps = PlayerPrefs.GetInt ("DoubleJumps");
 }
 
 void Update()
 {
     if (Input.touches.Length <= 0) {
         
     } 
     else 
     {
         for(int i = 0; i < Input.touchCount; i++)
         {
             if(this.guiTexture.HitTest (Input.GetTouch (i).position))
             {
                 if(Input.GetTouch(i).phase == TouchPhase.Ended)
                 {
                     if(lives > 0)
                     {
                         Application.LoadLevel (1);
                     }
                 }
             }
         }
     }
 }

}

Character Control

public class CharacterMove : MonoBehaviour {

 public float speed = 6.0f;
 Transform groundCheck;
 private float overlapRadius = 0.2f;
 public LayerMask whatIsGround;
 private bool grounded = false;
 private bool jump = false;
 public float jumpForce = 700f;
 private bool doubleJump = false;
 public int sJumpsCompleted = 0;
 public static int dJumpLimit = 0;
 public static int lives = 0;
 
 
 void Awake()
 {
     rigidbody2D.fixedAngle = true;
 }
 
 void Start()
 {
     groundCheck = transform.Find ("groundcheck");
     dJumpLimit = PlayerPrefs.GetInt ("DoubleJumps");
     lives = PlayerPrefs.GetInt ("Lives");
 }
 
 void Update()
 {
     if (Input.GetMouseButtonDown (0)) 
     {
         jump = true;
         sJumpsCompleted++;
     }
     if (sJumpsCompleted > 9) 
     {
         dJumpLimit = dJumpLimit + 1;
         PlayerPrefs.SetInt ("DoubleJumps", dJumpLimit);
         sJumpsCompleted = 0;
     }
 }
 
 void FixedUpdate()
 {
     
     //check if character is grounded
     grounded = Physics2D.OverlapCircle (groundCheck.position, overlapRadius, whatIsGround);
     
     //reset doublejump when player is on the ground
     if (grounded)
         doubleJump = false;
     //deactivate double jump when no jumps are available
     if (dJumpLimit < 1)
         doubleJump = true;
     
     //determine if player can jump
     bool canJump = (grounded || !doubleJump);
     
     if (jump && canJump) 
     {
         rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x,0);
         rigidbody2D.AddForce(new Vector2(0, jumpForce));
         if(!grounded && DoubleJumperManagerScript.dJumpLimit > 0)
         {
             doubleJump = true;
             dJumpLimit--;
             PlayerPrefs.SetInt ("DoubleJumps", dJumpLimit);
         }
     }
     
     jump = false;
     
     
     //apply forward movement
     rigidbody2D.velocity = new Vector2 (speed, rigidbody2D.velocity.y);
     
 }

}

Retry Button

public class RetryButtonScript : MonoBehaviour {

 void Update()
 {
     DoubleJumperManagerScript.dJumpLimit = CharacterMove.dJumpLimit;
     LivesManagerScript.lives = CharacterMove.lives;
     
     
     if (Input.touches.Length <= 0) {
         
     } 
     else 
     {
         for(int i = 0; i < Input.touchCount; i++)
         {
             if(this.guiTexture.HitTest (Input.GetTouch (i).position))
             {
                 if(Input.GetTouch(i).phase == TouchPhase.Ended)
                 {
                     if(CharacterMove.lives > 0)
                         Application.LoadLevel (1);
                 }
                 else if(CharacterMove.lives < 1)
                 {
                     Application.LoadLevel (0);
                 }
             }
         }
     }
 }

}

so basically i just want be able to have the lives work as they did and when lives run out to be able to reset the values for testing.

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 Mirakulous · Aug 06, 2014 at 03:25 PM 0
Share

ok so after rewriting code to go with your suggestion i found where the problem was. so would like to ask if you want to put it as an answer i will gladly mark it as correct

1 Reply

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

Answer by gjf · Jul 31, 2014 at 09:17 AM

this isn't an answer because i don't know where to begin.

it seems like you're using PlayerPrefs to access variables from other scripts rather than getting a reference to a single copy of them using GetComponent<>(), etc.

you should think about rewriting the code to do that - you'll have a lot less bugs, the code will be cleaner, easier to understand, and easier to maintain.

by all means, store variables that you want to persist between session using PlayerPrefs, but it's not intended for doing what you appear to be using it for.

also, your start button and retry button scripts are almost identical - consider using the same script for both and handle the loading separately. try to have all the loading done in one place too... for the same reason as accessing your variables from one place.

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 fatmanspineapple · Jan 14, 2015 at 10:29 PM 0
Share

I have a question similar to this which you may be able to answer. Please help- http://answers.unity3d.com/questions/874708/save-and-retrieve-score-not-working.html

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

22 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Playerprefs not saving 1 Answer

How to use PlayerPrefs to activate game objects permanently? 1 Answer

Can anyone help me figure out why PlayerPrefs wont save the money value?,Can anyone help me as to why my money won't save in my game? Using 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