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 haraldroine · Dec 31, 2014 at 06:16 AM · javascriptscorescore system

Score system is not working properly, help?

I am making an infinite runner game. In the game I have the score for the current run and the highscore.

The game is setup so that I spawn enemies on the right side. My character is on the left side. Both are sprites and they "spawn" Prefabs. The character spawns a shooting ball and the spawn on the right side spawn enemies. On the enemy prefab I have a script that check to see if the shooting ball that the character shot hit it. Once that happen I add 1 point to the score. If the score is higher than any other run before it should become the highscore.

The script to look for the hit between shots and enemies work. But the score system is not working.

I am using this script at the moment:

 static var score : int = 0;
 
 PlayerPrefs.SetInt("currentScore", 0);
 
 PlayerPrefs.SetString("PlayerScore", "Your Score: ");
 
 PlayerPrefs.Save();
 
 function OnTriggerEnter2D(obj : Collider2D) { 
 
   score += 1;
 
   if ( score > PlayerPrefs.GetInt("currentScore") {
 
   PlayerPrefs.SetInt("currentScore", score);
 
   }
 }

If the enemy hits the character it is game over and you come to another scene. On this scene I have a PLAY button that takes you back to the PlayScene. But once you start shooting enemies your score just adds upon the old score. It doen´t reset. I have tried to add reset(score=0;) in the Awake function but that makes the score 0 for each enemy that spawns.

For the game over scene I have this script attached to the PLAY button: Application.LoadLevel("MainScene");

I am really confused and can´t find what the issue is. I´ve been trying to wrap my head around this problem for way to many hours now.

Can someone please help me?

Comment
Add comment · Show 4
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 Landern · Dec 31, 2014 at 06:18 AM 0
Share

Is that literally your code? Does the PlayerPrefs calls actually live outside of a function? or did you remove the other code? Are you using PlayerPrefs.GetInt("currentScore") anywhere? Are you marking any objects as DontDestroyOnLoad?

avatar image taxvi · Dec 31, 2014 at 06:57 AM 1
Share

good point Larden, also, don't use PlayerPrefs so extensively. PlayerPrefs are meant to carry the data between the game launches, not during playing because they store everything on HD and reading/writing from HD is a slow process. create a separate game object to store the score.

avatar image haraldroine · Dec 31, 2014 at 09:19 AM 0
Share

Hmm.. How do I do so? I am new to unity and JS and C# so do not know how I can make a gameObject store my value.

avatar image haraldroine · Dec 31, 2014 at 02:00 PM 0
Share

Thank you all of the inputs. With your help and suggestions I managed to get it to work. I really appreciate your help!

Good luck coding!

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by gameplay4all · Dec 31, 2014 at 09:57 AM

You should indeed not reset the player prefs each time in the awake and as you've done in the 2nd and 3rd lines.

You should also call PlayerPrefs.Save(); in OnTriggerEnter2D.

I also don't get why you would save a string as standard as "Your score" in player preferences, but that is however irrelevant to the question.

Good luck!

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
avatar image
0

Answer by MiraiTunga · Dec 31, 2014 at 10:34 AM

Dont make score Static , does it have to be static ? , use

  `getComponent<theScoreScript>().score=something;`



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 haraldroine · Dec 31, 2014 at 12:21 PM 0
Share

If the Score is not static, the score does not update when the shot hits the enemy.

avatar image
0

Answer by webe · Dec 31, 2014 at 01:31 PM

This is what I used for my infinite sidescroller. I removed a couple of functions that get/set/print the values. In your case you might want to check for collision elsewhere, and if the colliding object is the Player, call the AddPoint() function. Hope it helps.

     int score = 0;
     int highScore = 0;
 
     void Start () {
         highScore = PlayerPrefs.GetInt("highScore", 0);
     }
 
     public void AddPoint(){
         score++;
 
         if (score > highScore) {
             highScore = score;
         }
     }
     
 
     void OnDestroy(){
         PlayerPrefs.SetInt ("highScore", highScore);
     }
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 MiraiTunga · Dec 31, 2014 at 05:34 PM 0
Share

shouldnt this line of code read

 `highScore = PlayerPrefs.GetInt("highScore");`

not

 highScore = PlayerPrefs.GetInt("highScore", 0);



avatar image webe · Dec 31, 2014 at 09:56 PM 0
Share

The 0 is the default value, which is used if nothing is found (first run for example)

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Reset score to 0 1 Answer

how do i attach GUITEXT to a prefab 1 Answer

Need Help with Dual Scoring System C# (score over time and kill score) 1 Answer

Help With Score Controller - Can You Only Have One? 1 Answer

How to add playerPrefs to this 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