Techshop

Unity Tutorial 22 - Adding the GameController

So we can display a score now… how about updating it….?

First off… where is the score held?

Let's think about how the score works.

The score is going to be your running total. The amount you spend and how much you get back.

When you press the 'fire' button you are going to have to lose money, the 'spawner' is responsible for that.

When you get a coin back, you're going to have to gain money, the 'collection' component is responsible for that.

Neither is directly responsible for each other. We are going to need a dedicated object that holds the user's score and is going to need to be informed of what's going on and update the running total.

We are going to have to create a script that can be informed of updates. And something is going to have to hold it.

It could be the 'Text' object itself. However, for this kind of case I prefer to abstract it out to an actual empty object.

I create a 'Empty' object in the root, and call it "GameController".

I also create a Component script on it called GameController, and move it to the scripts folder.

This Component script is going to have to update the score and have the functionality to be informed when the user drops a coin and when they receive one. This will use events and will be touched on in a bit.

First, updating the score. Which will need a score to start with.

Add a public field to the GameController, an int called funds with an initial value of 1000. This is going to be the user's… well… overall funds in pence, and they're going to have 1000 "pounds" to start with by default. It will present to the IDE so can be edited later if wanted.

Add a public field of type "Text" (Importing Unity.UI.Text) called fundsText. This is going to be the text field we update when the funds change.

We want a function to update the text field, parsing the pence into pounds

And we want the two public functions that take away funds and add funds. These need to be public as they will be called externally.

Overall, the class will end up looking something like...

public class GameController : MonoBehaviour {

	public int funds = 1000;

	public Text fundsText;
	
	private void Start () {
		UpdateText();
	}
	
	private void UpdateText() {
		fundsText.text = (funds / 100.0).ToString("F2");
	}

	public void SpendFunds() {
		funds -= 10;
	}

	public void AddFunds() {
		funds += 10;
	}
}

We call the UpdateText in the Start function, so it can update the Text object if we have a different initial value from what it's displaying.

The ToString("F2") is a fixed-format decimal display. There's always going to be the chance of rounding errors, but it's just to be quick for now.

Saving this we need to drag the "Text" object from the Canvas/Panel into the Game controller's "Funds Text" object. Just so it's bound to it now and there's no need for a null check in the code.

It's not really going to do anything at present. It'll update the funds if you change them and start the game. But that's all at present.

GIT TAG 018_AddingGameController

Unity - A simple tutorial < Unity Tutorial 21 - adding a GUI Unity Tutorial 23 - Scoring Linking with Events >