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 martinanugrah24 · Apr 07, 2017 at 10:44 AM · c#unity 2ddestroy objectcounterincrement

How to add score after destroying object?

Before this, I have made a scoring section to my program that used Invoke repeating every 1s. Now, I have problem how to make my scoring counter add an additional score when I destroy some objects.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class UIManager : MonoBehaviour
 {
 
     public Button[] buttons;
     public Button pauseButton;
     public Image[] images;
     public Text scoreText;
     public Text highScoreText;
     public Text yourScoreText;
     public Text text;
     bool gameOver;
     int score;
     levelscroller level;
     CoinMove cm;
     void Start()
     {
         gameOver = false;
         score = 0 + cm.plus;
         InvokeRepeating("scoreUpdate", 1.0f, 1.0f);
     }
     void Update()
     {
         storeHighScore(score);
         scoreText.text = "" + score;
         yourScoreText.text = "" + score;
         highScoreText.text = "" + PlayerPrefs.GetInt("highscore");
     }
     void scoreUpdate()
     {
         if (gameOver == false)
         {
             score += 1;
         }
     }
     void storeHighScore(int newHighscore)
     {
         int oldHighscore = PlayerPrefs.GetInt("highscore", 0);
         if (newHighscore > oldHighscore)
         {
             PlayerPrefs.SetInt("highscore", newHighscore);
             oldHighscore = newHighscore;
             PlayerPrefs.Save();
         }
     }

Another class:

 using UnityEngine;
 using System.Collections;
 public class CoinMove : MonoBehaviour
 {
     public float Speed;
     public int plus = 0;
     UIManager ui;
     void Start()
     {
 
     }
     void Update()
     {
         transform.Translate(new Vector3(0, -1, 0) * Speed * Time.deltaTime);
         //if (Input.touchCount > 0 || Input.GetTouch(0).phase == TouchPhase.Began)
         //{
         //    Destroy(transform.gameObject);  
         //}
     }
     private void OnMouseDown()
     {
         Destroy(gameObject);
         plus += 10;
     }
 }

It just make the counter of score totally 0.

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 PizzaPie · Apr 07, 2017 at 12:11 PM

You need a reference on the CoinMove script of the UIManager, then you need to make the score field public and access it from the CoinMove and add to it whatever you want. Anyway took the liberty to optimize your code a bit here:

 public class UIManager : MonoBehaviour
 {
 
     public Button[] buttons;
     public Button pauseButton;
     public Image[] images;
     public Text scoreText;
     public Text highScoreText;
     public Text yourScoreText;
     public Text text;
     bool gameOver;
     public int score;
     int initialScore;
     int oldHighScore;
     WaitForSeconds wfs;
     levelscroller level;
     string scoreString;
     void Start()
     {
         initialScore = score;
         oldHighScore = PlayerPrefs.GetInt("highscore", 0);
         highScoreText.text = oldHighScore.ToString();
         wfs = new WaitForSeconds(1f);
         gameOver = false;
         StartCoroutine(ScoreUpdate());
     }
     void Update()
     {
         if (gameOver)
             storeHighScore();
 
         if (score != initialScore)
         {
             scoreString = "" + score.ToString();
             scoreText.text = scoreString;
             yourScoreText.text = scoreString;
             if (score> oldHighScore)
             {
                 highScoreText.text = scoreString;
             }
             initialScore = score;
         }
 
     }
     IEnumerator ScoreUpdate()
     {
         while (!gameOver)
         {
             score += 1;
             yield return wfs;
         }
     }
     
     void storeHighScore()
     {
         if (score > oldHighScore)
         {
             PlayerPrefs.SetInt("highscore", score);
             PlayerPrefs.Save();
         }
     }
 }
 public class CoinMove : MonoBehaviour
 {
     public float Speed;
     public int bonus = 10;
     UIManager ui;
     public UIManager uiManager;     //set the reference in the Editor
     
     void Update()
     {
         transform.Translate(new Vector3(0, -1, 0) * Speed * Time.deltaTime);
         //if (Input.touchCount > 0 || Input.GetTouch(0).phase == TouchPhase.Began)
         //{
         //    Destroy(transform.gameObject);  
         //}
     }
     private void OnMouseDown()
     {
         uiManager.score += bonus;
         Destroy(gameObject);
     }
 }

Cheers, hope that helps. Note1: if you don't want to change your code that much just make the score field public, copy the CoinMove class and set the reference of the UIManager in each instance of the CoinMove and it should work.

Note2: the constant accessing of the registry is not good you only need to save the highscore once the game is over not on every frame.

Note3: it might have some errors didn't test it but you should get the logic behind it.

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

7 People are following this question.

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

Why is my Destroy not working in my foreach loop? 2 Answers

Delete object only when a certain cursor is enabled 1 Answer

c# unity increment power on click by multiplying 4 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