Unity - How to add score after destroying object in unity 2D?

2k views Asked by At

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 and not do the increment.

2

There are 2 answers

13
Kardux On

I don't really get the logic behind you int plus value and reference to CoinMove cm and UIManager ui...

The easiest way to achieve what you are trying would be to simply reference your UIManager with UIManager ui in your CoinMove script, add a new method to UIManager:

public void AddScore(int scoreToAdd)
{
    score += scoreToAdd;
}

And then when you want to destroy your coin in your CoinMove script, call for this method before destroying your object:

private void OnMouseDown()
{
    ui.AddScore(plus);
    Destroy(gameObject);
}

Hope this helps,

1
Nick Peelman On

First off:

  • Don't use PlayerPrefs every frame, behind the scenes, unity is using IO for saving the file. you'd want to save the highscore when the game is over.

  • in UIManager, you'd manage the score incrementing. Actually, it's best practice to handle Score incrementing in a controller (like a class called GameController) so the UI scripts only have mapping functions, (value to text fields for example). By splitting up controllers and UI, you can expand and change things more easily.

For the sake of simplicity let's ignore what I think is a cleaner version of programming.

Every object that holds a score value, can be added to the script (in your example, its CoinMove):

public int scoreValue;

This can be configured in the inspector to whatever value you want, or give it a standard value if you like.

CoinMove should have the following method:

public void AddScore(int amount) {
    score += amount;
}

Because you have a reference to UIManager in CoinMove, in coinMove we can do:

private void OnMouseDown()
  {
      Destroy(gameObject);
      ui.AddScore(scoreValue);
  }