Why is MaxGuessesAllowed still holding on to an old value?

So I had switched the max number of guesses from 10 to 5 and then back to 10 again, but my game still thinks that number is 5. I put some console statements to debug and noticed that it was saying:

5 max guesses
4 remaining guesses
3 remaining guesses
2 remaining guesses
1 remaining guesses
0 remaining guesses

using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class NumberWizard : MonoBehaviour
{
    private int _guess;
    private int _max;
    private int _min;
    private int _remainingGuesses;
    public int MaxGuessesAllowed = 10;

    public Text Text;

    private void Start()
    {
        StartGame();
    }

    private void StartGame()
    {
        _max = 1000;
        _min = 1;
        _guess = (_max + _min) / 2;
        _max = _max + 1;
        _remainingGuesses = MaxGuessesAllowed;
    }

    public void GuessHigher()
    {
        _min = _guess;
        NextGuess();
    }

    public void GuessLower()
    {
        _max = _guess;
        NextGuess();
    }

    private void NextGuess()
    {
        _guess = (_min + _max) / 2;
        Text.text = _guess.ToString();
        _remainingGuesses--;
        Debug.Log(MaxGuessesAllowed + " max guesses");
        Debug.Log(_remainingGuesses + " remaining guesses");
        if (_remainingGuesses <= 0) SceneManager.LoadScene("Win");
    }
}

one thing to bear in mind, if you make a variable public and you change the value in the inspector, this will override whats in code.

so where you have
public int MaxGuessesAllowed = 10;

so it will use the value in the inspector, even if you change the value in the code.

just one to be aware off.

2 Likes

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

Privacy & Terms