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 Vocus · Jul 04, 2015 at 04:59 AM · c#playerprefs

playerprefs and UI.

am i doing wrong? i want to the score increase on text UI when the object was destroyed, but when the object was destroyed, the score isn't increase or change.

     int score = 0;
     public Text ScoreText;
 
     void Start()
     {
         ScoreText.text = "Score: " + PlayerPrefs.GetInt("myScore");
     }
 
     void Update()
     {
         for (var i = 0; i < Input.touchCount; ++i)
         {
             if (Input.GetTouch(i).phase == TouchPhase.Began)
             {
                 RaycastHit hit;
                 Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position);
                 if (Physics.Raycast(ray, out hit))
                 Destroy(hit.collider.gameObject);
                 score += 1;
                 PlayerPrefs.SetInt("myScore", score);              
             }
         }
     }
 }

i have already assign the text UI on inspector of the object which i attached this script.

Comment
Add comment · Show 1
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 Priyanshu · Jul 04, 2015 at 05:37 AM 1
Share

you need to update Text every time an object is destroyed.

Since 'if' statement has no {}. Only next line will be executed. That is 'Destroy'.

Because of this 'score+=1' will be updated everytime A finger touchesthe screen even if the touch doesn't destroys Object.

Put destroy, score and playerpref lines inside {} after 'if'. And update score text also inside {} after you update score.

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Rhylvin2015 · Jul 04, 2015 at 05:04 AM

that's because you set your UI in void Start() which only happens the very first frame when you start the game. try putting in on your Update()

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
1

Answer by superbsumit · Jul 04, 2015 at 06:39 AM

In Update, you are only setting the 'PlayerPrefs', you have to update the 'ScoreText.text' value also, for displaying your score.

Also, increase the score only when the object gets destroyed as @Priyanshu suggested.

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 Vocus · Jul 04, 2015 at 09:53 AM

thank you guys. That very helpful

i just relize when i try @incorrect suggested. the score always increase when i touch the screen. then i try @Priyanshu suggest, i put the line destroy, score, scoreText, and playerprefs in {} after 'if' and it work.

     int score = 0;
     public Text ScoreText;
 
     void Start()
     {
         
     }
 
     void Update()
     {
         for (var i = 0; i < Input.touchCount; ++i)
         {
             if (Input.GetTouch(i).phase == TouchPhase.Began)
             {
                 RaycastHit hit;
                 Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position);
                 if (Physics.Raycast(ray, out hit))
                 {
                     Destroy(hit.collider.gameObject);
                     score += 1;
                     PlayerPrefs.SetInt("myScore", score);
                     ScoreText.text = "Score: " + score;
                 }  
             }
         }
     }
 }

thank you so much guys. :D

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 incorrect · Jul 04, 2015 at 02:43 PM 0
Share

No problem. But that was not Unity-specific question, it's general program$$anonymous$$g. Try to learn how to make algorithms and improve your skills. Good luck!

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

6 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Problems with saving/loading score with PlayerPrefs [C#] 1 Answer

Playerprefs not saving 1 Answer

How to use PlayerPrefs to activate game objects permanently? 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