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 Ful · Sep 25, 2014 at 01:35 PM · javascriptplayerprefsselectionhighscore

How do you run multiple Playerprefs scripts (JS)?

Hello everyone!

I just learned about using playerprefs and so I decided to try to implement a highscore system, and a selection feature, where before starting the game the player can choose between three different spaceships, once selected the game will load the main level with the desired spaceship.

I have managed to implement these three features; however, I am having a problem with my highscore script where the value always remains 0. I also noticed that whenever I disable the load the desired spaceship script in the main level, the highscore functions properly, and so it appears that these playerprefs scripts are somehow conflicting with each other. I suspect that the problem would have to do something with the function HighscoreUpdate in the highscore script.

Any help on how I could fix this problem would be highly appreciated and if any further information is required please let me know, also thanks in advance!


The Spaceship Selection Script in the main menu:

 / Menu buttons
 var logo                : GameObject;
 var playButton            : GUITexture;
 var helpButton            : GUITexture;
 var creditsButton        : GUITexture;
 
 // Additionaly Options
 
 var shipsSelectionOBJ    : GameObject;
 
 // Ship Selection Menu
 
 var backText            : GUIText;
 var shipSelectionText    : GUIText;
 var shipSlow            : GUITexture;
 var shipMid                : GUITexture;
 var shipFast            : GUITexture;
 
 // Statesments
 
 var backTextPressed        : boolean;
 var leveltoLoad            : String;
 var playBPressed        : boolean;
 var helpBPressed        : boolean;
 var creditsBPressed        : boolean;
 
 var shipSlowSelect        : boolean;
 var shipMidSelect        : boolean;
 var shipFastSelect        : boolean;
 
 // Saving Options
 
 function Update () {
 
     ShipSelection ();
 
 }

 function ShipSelection () {
 
     for ( var touch : Touch in Input.touches ) {
 
 if ( touch.phase == TouchPhase.Began && shipSlow.HitTest (touch.position) && !shipSlowSelect) { // This is spaceship option 1
         
                 shipMid.guiTexture.enabled            = false;
                 shipFast.guiTexture.enabled            = false;
                 
                 shipSlowSelect = true;
                 
                 PlayerPrefs.SetInt ("ShipSlow" ,1); // Save the selection of the player through giving it a name and value
                 
                 print ("Ship Slow selected!");
                 
                         backText.guiText.enabled = false;
                         
                         backTextPressed = true;
                 
                     yield WaitForSeconds (3);
                 
                 Application.LoadLevel (leveltoLoad);
                 
                     
         }
         
                 if ( touch.phase == TouchPhase.Began && shipMid.HitTest (touch.position) && !shipSlowSelect) { // This is spaceship option 2
         
                 shipSlow.guiTexture.enabled            = false;
                 shipFast.guiTexture.enabled            = false;
                 
                 shipSlowSelect = true;
                 
                 PlayerPrefs.SetInt ("ShipMid" ,2); // Save the selection of the player through giving it a and and value
                 
                 print ("Ship Mid selected!");
                 
                         backText.guiText.enabled = false;
                         
                         backTextPressed = true;
                 
                     yield WaitForSeconds (3);
                 
                 Application.LoadLevel (leveltoLoad);
                 
                     
         }
         
                 if ( touch.phase == TouchPhase.Began && shipFast.HitTest (touch.position) && !shipSlowSelect) { // This is spaceship option 3
         
                 shipSlow.guiTexture.enabled            = false;
                 shipMid.guiTexture.enabled            = false;
                 
                 shipSlowSelect = true;
                 
                 PlayerPrefs.SetInt ("ShipFast" ,3); // Save the selection of the player through giving it a and and value
                 
                 print ("Ship Fast selected!");
                 
                         backText.guiText.enabled = false;
                         
                         backTextPressed = true;
                 
                     yield WaitForSeconds (3);
                 
                 Application.LoadLevel (leveltoLoad);
                     
         }
     }
 }


The Spaceship loading script in the main level:

 var shipSlowSelected     : int = 0; // in order to enable the slow spaceship I must declare it as a interger...
 var shipMidSelected        : int = 0;
 var shipFastSelected    : int = 0;
 
 var shipSlow            : GameObject;
 var shipMid                : GameObject;
 var shipFast            : GameObject;
 var backText            : GUIText;
 
 // Depending on the ship selected one of the three will re-spawn...
 
 shipSlowSelected = PlayerPrefs.GetInt ("ShipSlow"); // Gets the int from the main menu which happens to be ShipSlow
                                                      
 shipMidSelected = PlayerPrefs.GetInt ("ShipMid");
 
 shipFastSelected = PlayerPrefs.GetInt ("ShipFast");
 
 
 function Update () {
     
     for ( var touch : Touch in Input.touches ) {
     
         if ( touch.phase == TouchPhase.Began && backText.HitTest (touch.position)) {
         
             Application.LoadLevel ("Menu");
             
             }
         }
         
     ShipSelector ();
     
         
     if (Input.GetKey ("up")); {
     
     PlayerPrefs.DeleteAll ();
     
     }
 }
 
 // Which ship did the player choose?
 
 function ShipSelector () {
     
     
     if ( shipSlowSelected == 1) { // if the playerpref value happens to be 1, then...
     
         shipSlow.renderer.enabled = true; // enable the slow spaceship
         //shipSlow.
     
     }
     
     if ( shipMidSelected == 2) { // if the playerpref value happens to be 2, then...
     
         shipMid.renderer.enabled = true; // enable the mid spaceship
         Debug.Log ("shipmid2");
     
     }
     
     if ( shipFastSelected == 3) { // if the playerpref value happens to be 3, then...
     
         shipFast.renderer.enabled = true; // enable the slow spaceship
         Debug.Log ("shipfast3");
     }
 }


Highscore & Timer script in the main level:

 // Timer
 
 var time                    : float;
 var timer                     : int;
 var timertext                 : String;
 var timeActive              : boolean;
 
 // HighScore
 
 var HighscoreText            : GUIText;
 var playername                 : String; // optional feature, still work in progress
 
 function Awake() {
 
 HighscoreText.text = "HighScore: " + PlayerPrefs.GetInt ("timerScore"); // at the start of the game the highscore will be displayed
 
 }
 
 function Update () {
 
     if (timeActive) {
     
         timer   = Time.timeSinceLevelLoad;
         time   += Time.deltaTime;
     }
     
      HighscoreUpdate ();
 }
 
 
 function OnGUI () { // though ongui shouldn't be used for mobile, a simple task like this one is fine
 
 //The gui-Time is the difference between the actual time and the start time.
 var minutes : int = time / 60; //Divide the guiTime by sixty to get the minutes.
 
 var seconds : int = time % 60;//Use the euclidean division for the seconds.
 
 var fraction : int = (time * 100) % 100;
  
     timertext = String.Format ("{0:00}:{1:00}:{2:00}", minutes, seconds, fraction);
  
     GetComponent(GUIText).text = timertext;
  
 }
 
 
 function StopTimer2 () {
     
     timer = 0;
     timeActive = false;
 
 }
 
 function HighscoreUpdate () {
 
     // If the value of timer happens to be more than what the current scroe is...

      if ( timer > PlayerPrefs.GetInt ( "timerScore" )) {

     // then update the new score, timerScore is a playerpref created which allows the player to save his highscore

             PlayerPrefs.SetInt ("timerScore", timer); // timer is the object that contains the value
 
         }
 }
 


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

1 Reply

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

Answer by HarshadK · Sep 25, 2014 at 01:53 PM

You need to perform a save in order for your changes to a variable to take effect. It is done using

 PlayerPrefs.Save();

So add this line after your line

 PlayerPrefs.SetInt ("timerScore", timer); // timer is the object that contains the value

from the last script above.

You also need to do the same where you set your ship variables.

Comment
Add comment · Show 3 · 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 Ful · Sep 25, 2014 at 02:46 PM 0
Share

Hi thanks for your reply!

I did as requested adding the PlayerPrefs.Save(); commend under the timerscore and to my ship int variables, unfortunately it did no difference.

The ship selection is indeed working because the data is being saved whenever I choice a ship. Additionally the highscore function only works when the spaceship loading script is disabled. The only thing I can think of is that perhaps declaring the ships as int 0 is somehow overriding the value of highscore to 0.

avatar image HarshadK · Sep 26, 2014 at 08:05 AM 1
Share

In your Spaceship loading script you have this code inside update()

 if (Input.Get$$anonymous$$ey ("up")); {
     PlayerPrefs.DeleteAll (); 
 }

So whenever you run your Spaceship selection script wheneveer you press your up key it deletes all your playerprefs and hence it return everything to zero including your highscore hence disabling your Spaceship script actually does not delete the highscore and it is correctly maintained. Delete that piece of code and then check. It will work properly. Also remember to save your playerprefs after changes.

avatar image Ful · Sep 28, 2014 at 10:15 AM 0
Share

Hi Harshadk,

I just wanted to let you know that your solution works! Thank you again for your time and patiences on helping me with this problem!

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

25 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

Related Questions

Highscoring and changing scripts (Java/UnityScript) 1 Answer

using playerprefs to update a guitext and record highscores 1 Answer

Im Doing HighScore System To My Game And There Is Something Wrong In My Scripts 0 Answers

Game/editor stops responding when going to Highscores 0 Answers

Does the || operator always work in UnityJS retrieval? 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