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 27, 2014 at 05:11 PM · c#playerprefslivesdoublejump

Setting intial value for playerprefs

hi all, sorry if this has been asked already but i'm stuck. im making a game that will have a lives manager and a double jump manager. i have set playprefs values for each of these and it works fine on game view but once i build an apk file it refuses to allow me to play as the lives value i set in the playerprefs isnt working so its setting lives to 0 meaning no lives to play game. below is my code for the relevant parts.

First the Lives and Double Jump managers

using UnityEngine; using System.Collections;

public class LivesManagerScript : MonoBehaviour {

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

}

using UnityEngine; using System.Collections;

public class DoubleJumperManagerScript : MonoBehaviour {

 public static int dJumpLimit = 30;

 void Awake()
 {
     DontDestroyOnLoad (gameObject);
 }

 void Start()
 {
     PlayerPrefs.SetInt ("DoubleJumps", dJumpLimit);
 }

 void Update()
 {
     PlayerPrefs.SetInt ("DoubleJumps", CharacterMove.dJumpLimit);
 }

}

next is start button

using UnityEngine; using System.Collections;

public class StartButtonScript : MonoBehaviour {

 public int lives = 0;
 
 void Awake()
 {
     lives = PlayerPrefs.GetInt ("Lives");
 }
 
 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);
                     }
                 }
             }
         }
     }
 }

}

the managers are on the scene after the start button is pressed. i tried this on the start button scene and that didnt work either. any help will be appreciated

Comment
Add comment · Show 3
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 CScherer549 · Jul 27, 2014 at 06:53 PM 0
Share

Have you tried debugging the lives value? What is Character$$anonymous$$ove? is it a character controller? are you sure it contains the proper value of lives and isn't defaulting to zero thus causing your lives to be set to 0 every update call?

avatar image Exbow · Jul 27, 2014 at 07:05 PM 0
Share

are you sure that when you build the scene that contains the object with the Lives$$anonymous$$anager is added to the build?

avatar image Mirakulous · Jul 28, 2014 at 05:09 AM 0
Share

Yeah it's my character controller. The thing is I figure it needs to be activated on the first scene but when I move it to that it doesn't give a value either. And yes it's added to the build

2 Replies

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

Answer by smallbit · Jul 28, 2014 at 06:15 AM

 //start
   PlayerPrefs.SetInt ("Lives", lives);
 //awake
   lives = PlayerPrefs.GetInt ("Lives");

Awake runs before start, hence you first try to read playerprefs which does not exist yet (it will be created in start). In your case when you read playerprefs you can do like this :

 lives = PlayerPrefs.GetInt ("Lives",6);
 

6 is a default value, in case your prefs does not exist yet, it will be given value of 6 (your value, not the playerprefs - this will still be nonexistent until you call SetInt on it)

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 Mirakulous · Jul 28, 2014 at 09:10 AM 0
Share

Will try once I get home and let you know the results

avatar image
1

Answer by Mirakulous · Jul 30, 2014 at 10:22 AM

ok so this worked thanks. now my problem is it no longer saves values when app is closed and reopened

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

26 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How To Add PlayerPrefs Scores? 1 Answer

Differences between mobile save/load data 1 Answer

How to save different values with same 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