Disclaimer:
I screen shot doesnt mean i steal content, just to show the issue i find.
this is my code:
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;
void Start()
{
StartGame();
}
void StartGame()
{
guess = Random.Range(min, max);
guessText.text = guess.ToString();
}
public void OnPressHigher()
{
min = guess;
NextGuess();
}
public void OnPressLower()
{
max = guess;
NextGuess();
}
// Use this for initialization
void NextGuess()
{
guess = (max + min + 1) / 2 ;
guessText.text = guess.ToString();
}
// Update is called once per frame
}
Compare with Rick
public class NumberWizard : MonoBehaviour
{
[SerializeField] int max;
[SerializeField] int min;
[SerializeField] TextMeshProUGUI guessText;
int guess;
void Start()
{
StartGame();
}
void StartGame()
{
NextGuess();
guessText.text = guess.ToString();
}
public void OnPressHigher()
{
min = guess + 1;
NextGuess();
}
public void OnPressLower()
{
max = guess - 1;
NextGuess();
}
void NextGuess()
{
guess = Random.Range(min,max+1);
guessText.text = guess.ToString();
}
}
The question is:
0. I misunderstanding about the updated same number.(ignore it if my code is not right.)
- Mine is stopped when I guess the right number.
- Rick’s code keeps updating when I push the higher.
but clarify, am I doing the right code or I miss something? - is this mean instead divide /2 the computer generate random number between max n min?