NumberWizard.cs - not working correctly

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

public class NumberWizard : MonoBehaviour {

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

int guess;

// Use this for initialization
void Start ()
{
    StartGame();
}

void StartGame()
{

    guess = (max + min) / 2;
    guessText.text = guess.ToString();
    max = min + 1;

}

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

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

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

}

}

Hello!!!
When I start the game, we see the number 500 on the screen.
After that I press the Higher button, the result is not plus, but subtracted. And on the contrary, if you press Lower, it will be added. Sorry for my English, I used a translator, for I am from Ukraine

Iā€™m not 100% sure but I think you need to remove
max = min + 1;
From the StartGame function.
Max should be set by the serialized field. Not by the StartGame function.

I looked up the code which roughly represents your code and there I see a line that says:
max = max + 1;
So I think you made a typo there. But still have no clue why this is even in. I think to not crash the game when no value is filled in so it is at least 1.

I remove max = min+1 from the StartGme() , the project started working properly.
Thanks for the help me)

1 Like

No problem! Glad to be of help!

I made a typo( max = min + 1;), I need this code(max = max+1;)My bad((

Privacy & Terms