Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 knightboat17 · Mar 18, 2016 at 12:27 PM · c#playerprefssave

Unable to get save score between levels working

I'm working on maze based game whereby the player moves around collecting tokens, which along with the distance travelled is added up to create a score, however I can't seem to get save score between levels to work, it just puts the score back to zero everytime. I'm using player prefs as recommended and followed a tutorial closely using the same code. What's odd is that it works for distance travelled on its own, just not score.

Heres my code

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 using UnityStandardAssets.CrossPlatformInput;
 
 
 public class PC1 : MonoBehaviour {
     
 
     public Text countText;
     public Text winText;
     public Text timerText;
     public Text scoreText;
     public Text distanceText;
 
 
     public int moveForce = 12;
     float Score = 0;
     public int seconds, minutes;
     float distanceTravelled = 0;
     Vector3 lastPosition;
 
     public GameObject Player;
     public GameObject other;
     
     Rigidbody rb;
 
     private int count;
 
     void Start ()
     {
         rb = GetComponent<Rigidbody>();
         count = 0;
         SetCountText ();
         winText.text = "";
         lastPosition = transform.position;
         if (PlayerPrefs.HasKey ("TotalScore")) 
         {
             if (Application.loadedLevel == 0) 
             {
                 PlayerPrefs.DeleteKey ("TotalScore");
             } 
             else 
             {
                 Score = PlayerPrefs.GetFloat ("TotalScore");
             }
         }
 
     }
 
     void FixedUpdate ()
     {
         Vector3 moveVec = new Vector3 (CrossPlatformInputManager.GetAxis ("Horizontal"), 0, CrossPlatformInputManager.GetAxis ("Vertical")) * moveForce;
 
         rb.AddForce (moveVec);
 
     }
 
 
     void Update ()
     {
 
         minutes = (int)(Time.time/60f);
         seconds = (int)(Time.time % 60f);
 
         distanceTravelled += Vector3.Distance (transform.position, lastPosition);//other.transform.position
         lastPosition = transform.position;
         Score = distanceTravelled + count * 10;
 
         scoreText.text = "Score = " + Score.ToString ("00");
         timerText.text = "Time : " + minutes.ToString ("00") + ":" + seconds.ToString ("00");
         distanceText.text = "Dist : " + distanceTravelled.ToString("f2");
 
     }
 
 
 
     void SaveScore()
     {
         PlayerPrefs.SetFloat ("TotalScore", Score);
     }
 
 
     void OnTriggerEnter(Collider other) 
     {
         if (other.gameObject.CompareTag ( "Pick Up"))
         {
             other.gameObject.SetActive (false);
             count = count + 1;
             SetCountText ();
 
         }
 
     
     }
 
     void SetCountText ()
     {
         
         countText.text = "Count: " + count.ToString ();
 
             if (count >= 6) {
 
 
                 int i = Application.loadedLevel;
                 System.IO.File.AppendAllText ("G:/ScoresList.txt", Score.ToString() + "/new/");
                 SaveScore ();
                 Application.LoadLevel (i + 1);
 
             }
         } 
 
 
 
 }




I tried removing count = 0 in the Start but still doesnt do anything.

Any hep would be much appreciated.

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
0

Answer by ForeignGod · Mar 18, 2016 at 12:29 PM

PlayerPrefs.Save and remove count = 0 on Start aswell.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How do I save and load the state of the GameObjects with PlayerPrefs? 0 Answers

Saving skill allocations for characters 1 Answer

Currency System Not Saving!? 1 Answer

Multiple Cars not working 1 Answer

How to create log by 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