ScoreCounter.cs
Assets > Scripts > ScoreCounter.cs
Assets > Scripts > ScoreCounter.cs
using UnityEngine;
public class ScoreCounter : MonoBehaviour
{
private SnakeTweaked snakeScript;
public GameObject snake;
GameObject[] FirstDigits = new GameObject[10];
GameObject[] SecondDigits = new GameObject[10];
GameObject[] ThirdDigits = new GameObject[10];
GameObject[] FourthDigits = new GameObject[10];
void Start()
{
snakeScript = snake.GetComponent<SnakeTweaked>();
FirstDigits[0] = transform.GetChild(0).gameObject.transform.GetChild(0).gameObject;
FirstDigits[1] = transform.GetChild(0).gameObject.transform.GetChild(1).gameObject;
FirstDigits[2] = transform.GetChild(0).gameObject.transform.GetChild(2).gameObject;
FirstDigits[3] = transform.GetChild(0).gameObject.transform.GetChild(3).gameObject;
FirstDigits[4] = transform.GetChild(0).gameObject.transform.GetChild(4).gameObject;
FirstDigits[5] = transform.GetChild(0).gameObject.transform.GetChild(5).gameObject;
FirstDigits[6] = transform.GetChild(0).gameObject.transform.GetChild(6).gameObject;
FirstDigits[7] = transform.GetChild(0).gameObject.transform.GetChild(7).gameObject;
FirstDigits[8] = transform.GetChild(0).gameObject.transform.GetChild(8).gameObject;
FirstDigits[9] = transform.GetChild(0).gameObject.transform.GetChild(9).gameObject;
SecondDigits[0] = transform.GetChild(1).gameObject.transform.GetChild(0).gameObject;
SecondDigits[1] = transform.GetChild(1).gameObject.transform.GetChild(1).gameObject;
SecondDigits[2] = transform.GetChild(1).gameObject.transform.GetChild(2).gameObject;
SecondDigits[3] = transform.GetChild(1).gameObject.transform.GetChild(3).gameObject;
SecondDigits[4] = transform.GetChild(1).gameObject.transform.GetChild(4).gameObject;
SecondDigits[5] = transform.GetChild(1).gameObject.transform.GetChild(5).gameObject;
SecondDigits[6] = transform.GetChild(1).gameObject.transform.GetChild(6).gameObject;
SecondDigits[7] = transform.GetChild(1).gameObject.transform.GetChild(7).gameObject;
SecondDigits[8] = transform.GetChild(1).gameObject.transform.GetChild(8).gameObject;
SecondDigits[9] = transform.GetChild(1).gameObject.transform.GetChild(9).gameObject;
ThirdDigits[0] = transform.GetChild(2).gameObject.transform.GetChild(0).gameObject;
ThirdDigits[1] = transform.GetChild(2).gameObject.transform.GetChild(1).gameObject;
ThirdDigits[2] = transform.GetChild(2).gameObject.transform.GetChild(2).gameObject;
ThirdDigits[3] = transform.GetChild(2).gameObject.transform.GetChild(3).gameObject;
ThirdDigits[4] = transform.GetChild(2).gameObject.transform.GetChild(4).gameObject;
ThirdDigits[5] = transform.GetChild(2).gameObject.transform.GetChild(5).gameObject;
ThirdDigits[6] = transform.GetChild(2).gameObject.transform.GetChild(6).gameObject;
ThirdDigits[7] = transform.GetChild(2).gameObject.transform.GetChild(7).gameObject;
ThirdDigits[8] = transform.GetChild(2).gameObject.transform.GetChild(8).gameObject;
ThirdDigits[9] = transform.GetChild(2).gameObject.transform.GetChild(9).gameObject;
FourthDigits[0] = transform.GetChild(3).gameObject.transform.GetChild(0).gameObject;
FourthDigits[1] = transform.GetChild(3).gameObject.transform.GetChild(1).gameObject;
FourthDigits[2] = transform.GetChild(3).gameObject.transform.GetChild(2).gameObject;
FourthDigits[3] = transform.GetChild(3).gameObject.transform.GetChild(3).gameObject;
FourthDigits[4] = transform.GetChild(3).gameObject.transform.GetChild(4).gameObject;
FourthDigits[5] = transform.GetChild(3).gameObject.transform.GetChild(5).gameObject;
FourthDigits[6] = transform.GetChild(3).gameObject.transform.GetChild(6).gameObject;
FourthDigits[7] = transform.GetChild(3).gameObject.transform.GetChild(7).gameObject;
FourthDigits[8] = transform.GetChild(3).gameObject.transform.GetChild(8).gameObject;
FourthDigits[9] = transform.GetChild(3).gameObject.transform.GetChild(9).gameObject;
Debug.Log("Testing arrays: " + FirstDigits[0] + " " + SecondDigits[1] + " " + ThirdDigits[2] + " " + FourthDigits[3]);
}
void Update()
{
switch (snakeScript.FourthDigitScore)
{
case 1:
FourthDigits[0].SetActive(false);
FourthDigits[1].SetActive(true);
break;
case 2:
FourthDigits[1].SetActive(false);
FourthDigits[2].SetActive(true);
break;
case 3:
FourthDigits[2].SetActive(false);
FourthDigits[3].SetActive(true);
break;
case 4:
FourthDigits[3].SetActive(false);
FourthDigits[4].SetActive(true);
break;
case 5:
FourthDigits[4].SetActive(false);
FourthDigits[5].SetActive(true);
break;
case 6:
FourthDigits[5].SetActive(false);
FourthDigits[6].SetActive(true);
break;
case 7:
FourthDigits[6].SetActive(false);
FourthDigits[7].SetActive(true);
break;
case 8:
FourthDigits[7].SetActive(false);
FourthDigits[8].SetActive(true);
break;
case 9:
FourthDigits[8].SetActive(false);
FourthDigits[9].SetActive(true);
break;
default:
FourthDigits[9].SetActive(false);
FourthDigits[0].SetActive(true);
break;
}
switch (snakeScript.ThirdDigitScore)
{
case 1:
ThirdDigits[0].SetActive(false);
ThirdDigits[1].SetActive(true);
break;
case 2:
ThirdDigits[1].SetActive(false);
ThirdDigits[2].SetActive(true);
break;
case 3:
ThirdDigits[2].SetActive(false);
ThirdDigits[3].SetActive(true);
break;
case 4:
ThirdDigits[3].SetActive(false);
ThirdDigits[4].SetActive(true);
break;
case 5:
ThirdDigits[4].SetActive(false);
ThirdDigits[5].SetActive(true);
break;
case 6:
ThirdDigits[5].SetActive(false);
ThirdDigits[6].SetActive(true);
break;
case 7:
ThirdDigits[6].SetActive(false);
ThirdDigits[7].SetActive(true);
break;
case 8:
ThirdDigits[7].SetActive(false);
ThirdDigits[8].SetActive(true);
break;
case 9:
ThirdDigits[8].SetActive(false);
ThirdDigits[9].SetActive(true);
break;
default:
ThirdDigits[9].SetActive(false);
ThirdDigits[0].SetActive(true);
break;
}
switch (snakeScript.SecondDigitScore)
{
case 1:
SecondDigits[0].SetActive(false);
SecondDigits[1].SetActive(true);
break;
case 2:
SecondDigits[1].SetActive(false);
SecondDigits[2].SetActive(true);
break;
case 3:
SecondDigits[2].SetActive(false);
SecondDigits[3].SetActive(true);
break;
case 4:
SecondDigits[3].SetActive(false);
SecondDigits[4].SetActive(true);
break;
case 5:
SecondDigits[4].SetActive(false);
SecondDigits[5].SetActive(true);
break;
case 6:
SecondDigits[5].SetActive(false);
SecondDigits[6].SetActive(true);
break;
case 7:
SecondDigits[6].SetActive(false);
SecondDigits[7].SetActive(true);
break;
case 8:
SecondDigits[7].SetActive(false);
SecondDigits[8].SetActive(true);
break;
case 9:
SecondDigits[8].SetActive(false);
SecondDigits[9].SetActive(true);
break;
default:
SecondDigits[9].SetActive(false);
SecondDigits[0].SetActive(true);
break;
}
switch (snakeScript.FirstDigitScore)
{
case 1:
FirstDigits[0].SetActive(false);
FirstDigits[1].SetActive(true);
break;
case 2:
FirstDigits[1].SetActive(false);
FirstDigits[2].SetActive(true);
break;
case 3:
FirstDigits[2].SetActive(false);
FirstDigits[3].SetActive(true);
break;
case 4:
FirstDigits[3].SetActive(false);
FirstDigits[4].SetActive(true);
break;
case 5:
FirstDigits[4].SetActive(false);
FirstDigits[5].SetActive(true);
break;
case 6:
FirstDigits[5].SetActive(false);
FirstDigits[6].SetActive(true);
break;
case 7:
FirstDigits[6].SetActive(false);
FirstDigits[7].SetActive(true);
break;
case 8:
FirstDigits[7].SetActive(false);
FirstDigits[8].SetActive(true);
break;
case 9:
FirstDigits[8].SetActive(false);
FirstDigits[9].SetActive(true);
break;
default:
FirstDigits[9].SetActive(false);
FirstDigits[0].SetActive(true);
break;
}
}
}
This code will be explained in sections, which each have a different colour to separate them from each other. Comments in the script have been removed from this excerpt, but will be included in the Google Doc containing the script, linked at the bottom of the page.
SECTION 1: Setting up
This part of the script is for setting up the namespaces and variables to be used in the script.
using UnityEngine
In the namespaces section, only the UnityEngine namespace is needed for this script. All other namespaces are unnecessary, including the default System.Collections and System.Collections.Generic.
private SnakeTweaked snakeScript
public GameObject snake
These two variables are for making sure the script is called once the conditions in the SnakeTweaked script for this script to be called is triggered. The SnakeTweaked variable is private as it doesn't need to be shown in the inspector and only needs to be used in this script.
GameObject[] FirstDigits = new GameObject[10]
GameObject[] SecondDigits = new GameObject[10]
GameObject[] ThirdDigits = new GameObject[10]
GameObject[] FourthDigits = new GameObject[10]
These variables are special, in that I've never used them in any previous scripts before. These are array variables. The structure of these variables in this case are the type of variable first (e.g. int, float, or in this case, GameObject), then the name of the array (in this case, I named it FirstDigits, SecondDigits, etc.), then the declaration of 10 newly created GameObjects that belong to the array. Because nothing has been declared about these new GameObjects yet, these GameObjects are empty.
SECTION 2: Declaring what calling each variable does
This part of the script is for declaring what calling each variable and each part of the array on their own actually does. This makes it easier for calling these variables in the future as it saves the step of having to call a function to find the GameObject child of the GameObject that this script is attached to, every single time the variables are called, before calling the actually intended functions.
void Start()
The Start void calls the functions under it as soon as the game starts (provided the script is enabled in the first place) and then never again, unless the script is restarted.
snakeScript = snake.GetComponent<SnakeTweaked>()
This line declares what calling the variable alone actually does. In this case, every time snakeScript is called, it will go to the Snake GameObject and get the SnakeTweaked component from it.
FirstDigits[0] = transform.GetChild(0).gameObject.transform.GetChild(0).gameObject
This tells the script what calling each part of the array does. In this case, it will get the first child GameObject of the GameObject that this script is attached to (in this case, the First Digit GameObject), and then converts the function for UnityEngine.GameObject (this gets rid of an annoying error caused by the script thinking this function is for another type of variable), before then getting the first child GameObject of that child GameObject (in this case, the Number 0 GameObject).
This is then repeated 10 times to cover every number in the digit, before then being repeated for all four digits. This is why this section looks so long, but trust me, it's the only clean way to do this as far as my knowledge goes.
Debug.Log("Testing arrays: " + FirstDigits[0] + " " + SecondDigits[1] + " " + ThirdDigits[2] + " " + FourthDigits[3])
Just before moving onto making the score counter actually work in the Update method, this line allows me to check if the arrays are actually working as intended. This will get the value of the first part of the FirstDigits array, then the second part of the SecondDigits array, then the third part of the ThirdDigits array, and then finally the fourth part of the FourthDigits array. It will then print this to the console, and if it works, then the script will proceed and work as intended. This is just for debugging.
A flow chart that shows the order of operations for each function in this section, simplified down to remove the repeated functions.
SECTION 3: Making the score counter tick over
This is the most important part of the script as it allows the score counter to actually work as intended, as far as visuals are concerned.
void Update()
The Update void calls all the functions under it for every frame, indefinitely, until the script is disabled.
switch (snakeScript.FourthDigitScore)
This line is also special for the same reason the array variables are special, and that's because this is also the first time I've ever used this function. This is a switch statement, which act like streamline conditionals (like if/else statements). They are useful for when you want to compare a single variable against a series of constants. Basically, it works similar to if/else statements if you're just using it to compare variables, except they're a LOT better in terms of keeping the code clean and running smoother.
This specific switch statement is for the FourthDigitScore variable found in the SnakeTweaked script (see how snakeScript is called alone and the script knows what to do).
case 1:
FourthDigits[0].SetActive(false)
FourthDigits[1].SetActive(true)
break
These cases for the switch statement tell the script what to do if the value of the variable that this switch statement is for is at a certain value. In this case, if the FourthDigitScore value is 1, the script will go to the first case, which will then call the first part of the FourthDigits array and disable the GameObject within it (the Number 0 GameObject) using SetActive and will then call the second part of the FourthDigits array and enable the GameObject within it (the Number 1 GameObject) using SetActive. Once this is done, the case will then break, letting the script know that the set of functions within it have finished.
This process is then repeated for each number in the digit to make sure the correct number in the digit is shown on screen when the script is called, and then it is repeated for all four digits to make sure the score counter works as intended for all digits, counting up to 9999.
default:
FourthDigits[9].SetActive(false)
FourthDigits[0].SetActive(true)
break
This case is different from all the rest because it's the default case, which is the case that the switch statement falls back to if the value of the variable that this switch statement is for goes outside of the expected number range. In this case, if the FourthDigitScore variable is set to 0, 10 or any other value that isn't between 1–9, it will fall back to this case.
Because of the way that the script is set up, the first three digits of the score counter will reset back to 0 if their respective score reaches 10, making it impossible for the value to go any further outside of the expected number range than 0 and 10.
A flow chart that shows the order of operations for each function in this section, simplified down to remove the repeated functions.
The full script
This the full ScoreCounter.cs script, straight from the project. This version of the script includes the original colouring and also has the original comments that were removed from the code above.