How to create a score update system in games and apps with Unity and C#. Appreciate how we could separate decision-making from implementation in coding and henceforth, creates a reusable architecture that could improve workflow. The approach is used in the development an Android app called CountPad and the game Splotches. CountPad is a work-in-progress.

Two scripts/classes are used in this CounterPad application:

  • LevelManager.cs – the ‘brain’ that controls the mechanics, and contains methods and variable (Integer: oneTapScore). Other game objects called on the methods in this script to perform certain functions. An example would be a button that update score with each press. The LevelManager script is attached to an empty game object. The game object is referenced by the Add/Substract buttons when pressed. It focuses on decision-making and resource allocation.
  • Scores.cs – It runs the module that determines if the current button’s action should lead to a valid update and update the current score when the condition called for it. The Scores.cs script is attached to both the Add and Subtract Buttons. Its primary purpose is implementation.

Level Manager (The Brain)

The LevelManager.cs script is attached to an empty game object. It holds a public interger, oneTapScore, which value is accessible and adjustable via Unity’s Inspector at run time (a default setting for public variables). It also holds 2 methods, the¬†Add/Subtract methods.

UnityLMgerGameObject

using UnityEngine;

public class LevelManager : MonoBehaviour {

    // Use this for initialization
    public int oneTapScore = 1;

    //Method activated when the Add Button is pressed.
    public void AddToCurrentScore()
    {
        Scores.main.UpdateScoreText(oneTapScore);
    }

    //Method activated when the Subtract Button is pressed.
    public void SubtractFromCurrentScore()
    {
        Scores.main.UpdateScoreText(-oneTapScore);
    }

    public void QuitApp()
    {
        Application.Quit();
    }

    
}

Scores Script (Score Updating Module)

The Scores.cs script is where the implementation of score update takes place. The script is attached to 2 Game Objects: ButtonAdd and ButtonSubtract separately. The onClick() event of the button calls on the empty game object to access the Add/Subtract methods located accordingly in the LevelManager.cs script (attached to the empty game object) to activate the update. Within these 2 methods, the UpdateScoreText funtion from the Scores.cs script is then called, and the values of tap count, passed¬†through Scores.cs’ UpdateScoreText method as the required parameter (integer) .

ButtonAdd

using UnityEngine;
using UnityEngine.UI;

public class Scores : MonoBehaviour {

    // Use this for initialization
    private Text score;
    private string scoreText;
    private int currentScore = 0;
        
    public bool isCurrentScoreNothing;

    public static Scores main;
    void Awake()
    {
        main = this;
    }

    void Start () {
        score = this.GetComponent();
	}
	
    //Receive the latest score from LevelManager
    //add to current score for update.
    //Parse and convert integer from text string vice versa
    public void UpdateScoreText(int latestScore)
    {
        currentScore = int.Parse(score.text);
        currentScore += latestScore;
        
        //No update if score<0 if (currentScore>= 0)
        {
            score.text = currentScore.ToString();
            isCurrentScoreNothing = false;
        }
        else
        {
            isCurrentScoreNothing = true;
        }
        
    }
}

Separating the decision making in LevelManager.cs (what values should be allocated to each tap on the button) from the implementation in Scores.cs (when and how the values should be updated) has several advantages. It reduces the amount of coding and keeps the lines of coding clean and easy to understand. It improves the portability and re-usability of implementation codes by keeping references generic. Just drop the same Scores.cs file into future application with the same purpose. Much of the challenges in coding can be overcome with some organization.

Published by Trine Epiphany

Leave a Reply

Your email address will not be published. Required fields are marked *