NumberWizard second guess is always zero

When I try and run Number Wizard it starts off fine, with a guess of 500 but the second guess is always 0 no matter if I pressed the higher or lower button. I’ve checked the code but can’t find anything wrong, is there anything else that could be the problem?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class NumberWizardScript : MonoBehaviour
{

[SerializeField] int max;
[SerializeField] int min;
[SerializeField] TextMeshProUGUI guessText;

int guess;

// Start is called before the first frame update
void Start()
{
    StartGame();
}

void StartGame()
{
    guess = (max + min) / 2;
    guessText.text = guess.ToString();
    max = max + 1;
}

public void OnPressHigher()
{
    min = guess;
    NextGuess();
}

public void OnPressLower()
{
    max = guess;
    NextGuess();
}

void NextGuess()
{
    guess = (max + min) / 2;
    guessText.text = guess.ToString();
}

}

Hi Sophie,

What values do max and min have in your Inspector? Have you already used Debug.Logs in your code to log the values into your Console?

Hi Nina,

The values in the Inspector are 0 and 1000 from min and max, I’ve also tried with 1 and 1000 but nothing has worked. I think I have used Debug.Logs but I’m not certain, that’s just the part of the code that allows the min and max to show up on the console, right?

We need to figure out where the issue occurs. For this reason, it’s a good idea to log min, max and guess into your console. Also add a meaningful message.

Since the initial guess is 500, something must have gone wrong because the following ones are always 0. At some point, (max + min) must range between 0 and 1.9999. Otherwise, guess cannot be 0.

This topic was automatically closed after 14 days. New replies are no longer allowed.

Privacy & Terms