• 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 Tyler Starr · Mar 06, 2011 at 05:11 AM · guiiphoneplayerprefsfixedupdate

PlayerPrefs aren't working in iphone

In Keeping score and recording the score with this script:

var score = 0; var scoreText = "Score: 0"; var mySkin : GUISkin;

function OnTriggerEnter( other : Collider ) { Debug.Log("OnTriggerEnter() was called"); if (other.tag == "Coin") { Debug.Log("Other object is a coin"); score += 100; scoreText = "Score: " + score; Debug.Log("Score is now " + score); Destroy(other.gameObject); }

}

function FixedUpdate(){ Debug.Log("OnTriggerEnter() was called"); score += 1;

     scoreText = "Score: " + score;
     Debug.Log("Score is now " + score);
     if (score > PlayerPrefs.GetInt ("Points")) {

     PlayerPrefs.SetInt("Points", score);    

} }

function OnGUI () { GUI.skin = mySkin; GUI.Label (Rect (10, 10, 500, 200), scoreText.ToString()); }

And I'M calling the "Points" Number with :

var score = 0; var scoreText = "Score: 0"; var mySkin : GUISkin;

function Start() { PlayerPrefs.GetInt ("Points");

    scoreText = "High Score: " +     PlayerPrefs.GetInt ("Points");



 }

function OnGUI () {

 GUI.skin = mySkin;
  GUI.Label (Rect (500, 10, 200, 500), scoreText.ToString()); }

-These scripts work in the unity editor. when the score is more than the best score it is changed. But when it is built and put on the device the score GUI isn't displayed and "Best Score" system doesn't work. How can i use fix this? Why isn't PlayerPrefs working on iPhone?

Im sorry about the code formatting I can't get it formatted no matter how hard I try.

-Thanks

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 Eric5h5 · Mar 06, 2011 at 05:43 AM 0
Share

Select the code and hit the code format button.

avatar image Tyler Starr · Mar 06, 2011 at 06:48 AM 0
Share

Thanks it is formatted!

avatar image cregox · Aug 20, 2012 at 06:36 PM 0
Share

related: http://answers.unity3d.com/questions/11112/where-on-iphone-editor-are-playerprefs-stored.html

2 Replies

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

Answer by Eric5h5 · Mar 06, 2011 at 07:16 AM

Remove the FixedUpdate function; it runs every physics frame, which is 50fps by default. That means you have 100 Debug.Log and 50 PlayerPrefs.SetInt calls per second, which is going to be extremely slow. You should use GetInt once (when starting), and SetInt once (when saving). The score only needs to be set when it changes. Only use FixedUpdate for moving physics objects or applying forces. Also, you don't need to use ToString for something that's already a string.

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 Tyler Starr · Mar 06, 2011 at 07:34 AM 0
Share

The High score isn't displayed on the iPhone, but it shows it in the editor. Why would this happen and how can i fix it?

avatar image Eric5h5 · Mar 06, 2011 at 08:00 AM 0
Share

Fix the code so it works properly first. Remember, call PlayerPrefs once, and store the results in a variable.

avatar image
0

Answer by Tyler Starr · Mar 07, 2011 at 06:45 AM

I feel stupid, but the problem was that the GUI was in the camera view in the editor. And on the device it was out of camera view. All I had to do was move the GUI! Thanks Eric I changed my scripts in the way you suggested. Thanks.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

how big is my app size 2 Answers

How to drag a scrollView with TouchPhase.Moved? 0 Answers

make game work on older iphone 1 Answer

useGUILayout=false does not honor GUI.depth 0 Answers

Exporting to Iphone, bad changes... 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges