Script not using established value

I am doing a tutorial and the script allows me to start with a number of guess and reduce it by one, but when i have the variable as public, it does not persist the assigned value and I don’t understand why. Could someone explain this for me please.

Reference line 5 below, when maxGuessesAllowed is public it is read as 10, but if it is not public, it takes the assigned value.

here is the code:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class NumberWizard : MonoBehaviour {

int max;
int min;
int guess;

int maxGuessesAllowed = 2;

public Text text;

// Use this for initialization
void Start () {
	StartGame();
}
void StartGame(){
	Debug.Log(maxGuessesAllowed);
	max = 1000;
	min = 1;
	
	NextGuess();
	Debug.Log(maxGuessesAllowed);
}

public void GuessHigher(){
	min = guess;
	Debug.Log(maxGuessesAllowed);
	NextGuess();
}

public void GuessLower(){
	max = guess;
	Debug.Log(maxGuessesAllowed);
	NextGuess();
}


void NextGuess (){
	Debug.Log(maxGuessesAllowed);
	guess = Random.Range(min, max + 1);
	text.text = guess.ToString();
	maxGuessesAllowed = maxGuessesAllowed - 1;
	Debug.Log(maxGuessesAllowed);
	if(maxGuessesAllowed == 0) {
		Application.LoadLevel("Win");
	}
}

}

This is due to the serialization of public variables in Unity. If you set the variable to be private, Unity does not expect the value to come from inspector so it will use the script value. If you set the variable to public or “[SerializeField] private” then Unity considers the script only value only the first time. Once you set it in Inpsector, the script value is no longer registered. You can press Reset in the component gear icon to tell the serializer to use the script value again, but set it once in inspector and it is done again.

Just how Unity serializes script values.

P.S: Sorry about the layout of the answer, Unity Answer is not allowing to skip lines…