Using ToString - Slightly confused why reversing this works

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();
}

}

Folow up:

I solved it! I realised it was because I had put my numbers into the serialised fields in Unity the wrong way round. Max was 1 and Min was 1000! I figured this out after the next lesson where we look at the changing numbers in those fields as we play the game.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms