I’m coming to the end of the Number Wizard module but don’t seem to be getting the results I’d expected.
It all works fine until the game is restarted. The ‘max’ won’t reset to 1000 and seems to be stuck of the ‘guess’ number. I’ve tried moving my integers into ‘Start ()’ and also after the print ‘I WON’ text to see if that would reset them but both just cause me more errors.
So, you want to define the integers at the top of the class, so that they are member variables and can be accessed anywhere in the script, but the initialisation of the variables you want to move to where the game begins, in the example below, they are initialised within the StartGame method, which is called from Start.
Example;
public class NumberWizard: MonoBehaviour
{
int max;
int min;
int guess;
/// <summary>
/// Initialise
/// </summary>
void Start()
{
StartGame();
}
/// <summary>
/// Start the game
/// </summary>
void StartGame()
{
max = 1000;
min = 1;
guess = 500;
// ...
}
// ...
}