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 cliffmlimwa · Jun 09, 2018 at 11:10 AM · destroydestroy objectcountscores

count score start 0

i create game, when a ball touch a object score.text show the score=10 but i have a problem when the game is over and i start again to play the ball when touch object its start score 10 again etc i don't want it i need wen game over and i touch again a ball score continue to count where its finished like when a score is 60 and game over and i pray again score start to 70 ,

 public class BallContollers : MonoBehaviour {
 public int scorevalue;

Ballcontroler.cs

     private GameManager gameManager;
 
 void OnTriggerEnter(Collider col){
 
 
         if (col.gameObject.tag == "Diamond") {
 
 
 
              
             Destroy (col.gameObject);
             Destroy (part, 1f);
 
 
                 gameManager.AddScore (scorevalue);
         
 
 
             
                 
                 
 
         }
 
     }

gamemanager.cs

 public Text scoreText;
 
     private int Scores;
 
 void Start () {
         
          Scores = 0;
         
         scoreText.text = PlayerPrefs.GetInt ("ScoreText").ToString();
         PlayerPrefs.SetInt ("ScoreText", Scores);
 
     }
 
 public void updatescore(){
         if (Scores > PlayerPrefs.GetInt ("ScoreText")) {
             //IncrementScore ();
 
         }
         scoreText.text = "" + Scores;
 
     }
     public void AddScore(int newscorevalue){
         
 
                 Scores+= newscorevalue;
 
 
         PlayerPrefs.SetInt ("ScoreText", Scores);
 
     
         updatescore ();
 }
 }
 
 
     






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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by _samuel12345 · Jun 09, 2018 at 12:35 PM

You are using Player Prefs to save the score value and then are setting score to it when you start the game. If you want the score to start at 0 every time the game starts then you have no need to use Player Prefs.

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 cliffmlimwa · Jun 09, 2018 at 01:19 PM 0
Share

hii samuel12345 thanks but i don't wan't score start to 0 when i play again i want score to continue again and again i don' t know whats wrong with code

avatar image
1

Answer by fluxhackspro · Jun 09, 2018 at 09:00 PM

Well in the start method you set the "Score" to 0 and then already save it as the new playerPrefs Score, thats why it's always the same. Then when you update the score you have to set it again in PlayerPrefs

Under IncrementScore(); you have to set the new score to the playerprefs:

   if (Scores > PlayerPrefs.GetInt ("ScoreText")) {
                  //IncrementScore ();
                   PlayerPrefs.SetInt ("ScoreText", Scores);
              }

 



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

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

Cannot remove sprite using destroy 1 Answer

Destroy Clone when colliding with 2d box collider 0 Answers

Destroy function not documented? 2 Answers

Unity createNew Game Object after calling a Destroy() 1 Answer

destroying cloned prefab not working as intended 0 Answers


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