I just finished the Using ToString lesson in the Number Wizard UI section of the 2D course. My game is working perfectly (yay!), however I’m not sure why it’s working.
When I hooked up the buttons to the code and pressed higher, the next guess was lower. I reversed the maxGuess and minGuess lines in the OnPressHigher and OnPressLower methods and bam! It worked.
However, this is backwards to what is shown on the lesson so my question is: Why does this work?
public class NumberWizard : MonoBehaviour
{
[SerializeField] int maxGuess;
[SerializeField] int minGuess;
// This lets us access the TextMeshProUGUI field (where you write your text in Unity)
[SerializeField] TextMeshProUGUI guessText;
int guess;
// Start is called before the first frame update
void Start()
{
StartGame();
}
public void OnPressHigher()
{
maxGuess = guess;
NextGuess();
}
public void OnPressLower()
{
minGuess = guess;
NextGuess();
}
void StartGame()
{
guess = (minGuess + maxGuess) / 2;
guessText.text = guess.ToString();
maxGuess = maxGuess + 1;
}
void NextGuess()
{
guess = (minGuess + maxGuess) / 2;
guessText.text = guess.ToString();
}
}