Working with scores – scripting with text
To display a score in the GUI, we'll first need score functionality, that is, code to create a score system. Essentially, the score functionality will be added to a general, overarching GameController
class, responsible for all game-wide logic and features. The code for GameController
and its score feature set is included in Code Sample 4.4, as follows. This file should be added to the Scripts
folder of the project:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
//------------------------------
public class GameController : MonoBehaviour
{
//Game score
public static int Score;
//Prefix
public string ScorePrefix = string.Empty;ic
//Score text object
public Text ScoreText = null;
//Game over text
public Text GameOverText = null;
public static GameController ThisInstance = null;
//------------------------------
void Awake()
{
ThisInstance = this;
}
//------------------------------
void Update...