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
1
Question by SirSmileX · Jul 21, 2017 at 02:47 PM · playerprefsscore

Problem with PlayerPrefs

Hey guys, so I want to store the current score of my Pong game with PlayerPrefs so that I can load the scene (after a goal) again and display the score. But somehow it just gives my 0:0 everytime someone makes a goal. Maybe someone can help me.

Ball Script (OnCollisionEnter2D is important)

 using UnityEngine;
 
 public class Ball : MonoBehaviour {
 
     public float startSpeed;
     public float collisionSpeedMultiplier;
     public GameManager gm;
 
     private Rigidbody2D rb;
 
     void Awake () 
     {    
         gm = Camera.main.GetComponent<GameManager> ();
         rb = GetComponent<Rigidbody2D> ();
 
         float angle = Random.Range (120f, 240f) + Random.Range (0f, 2f) * 180f;
         float radAngle = angle * Mathf.Deg2Rad;
 
         Vector2 startDir = new Vector2 (Mathf.Cos (radAngle), Mathf.Sin (radAngle));
         rb.velocity = startDir * startSpeed;
     }
 
     void OnCollisionEnter2D (Collision2D col)
     {
         rb.AddForce (rb.velocity.normalized * collisionSpeedMultiplier, ForceMode2D.Impulse);
 
         if (col.gameObject.CompareTag ("TriggerRight")) {
             PlayerPrefs.SetInt ("pointsP1", gm.pointsP1++);
             gm.GoalPoint ();
 
         } else if (col.gameObject.CompareTag ("TriggerLeft")) {
             PlayerPrefs.SetInt ("pointsP2", gm.pointsP2++);
             gm.GoalPoint ();
         }
     }
 }

GameManager Script:

 using UnityEngine;
 using UnityEngine.SceneManagement;
 using UnityEngine.UI;
 
 public class GameManager : MonoBehaviour {
 
     [SerializeField]
     public int pointsP1;
     [SerializeField]
     public int pointsP2;
 
     public Text score;
 
     void Start ()
     {
         pointsP1 = PlayerPrefs.GetInt ("pointsP1");
         pointsP2 = PlayerPrefs.GetInt ("pointsP2");
 
         Debug.Log (pointsP1 + ":" + pointsP2);
 
         score.text = pointsP1 + ":" + pointsP2;
     }
 
     public void GoalPoint ()
     {
         SceneManager.LoadScene (SceneManager.GetActiveScene ().buildIndex);
     }
 }
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 NoBrainer-David · Jul 21, 2017 at 02:50 PM

Change

 PlayerPrefs.SetInt ("pointsP1", gm.pointsP1++);


to

 gm.pointsP1++;
 PlayerPrefs.SetInt ("pointsP1",gm.pointsP1 );

For why, see this question on StackOverflow:

https://stackoverflow.com/questions/24853/what-is-the-difference-between-i-and-i

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 Bunny83 · Jul 21, 2017 at 02:58 PM 0
Share

Right, another alternative is:

 PlayerPrefs.SetInt ("pointsP1", ++gm.pointsP1);
avatar image SirSmileX · Jul 21, 2017 at 03:01 PM 0
Share

Thank you!

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

72 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 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 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

Guide in ArrayPrefs or PlayerPrefx? 1 Answer

DontDestroyOnLoad reset the gameobject 1 Answer

How would I make a high score thing? (it would be on a seperate scene) 2 Answers

PlayerPrefs Highscore problem 1 Answer

calculate playerprefs and display 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