Number wizard game, when I press Enter game restarts,

Hi,
Please help me to locate my mistake
In the number Wizard game I’ve programed buttons to play the game, everything seems to work, but the return button. It should Indicate that the game Is over, but it restarts the game for some reason
Please assist

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

public class NumberWizzard : MonoBehaviour
{

    int max;
    int min;
    int guess;

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


    void StartGame()
    {
        max = 1000;
        min = 1;
        guess = 500;

        Debug.Log("G'day mate, welcome to numba wiz-ud...");
        Debug.Log("Pick a number, don't tell me what it is...");
        Debug.Log("The highest number you can pick is: " + max);
        Debug.Log("The lowest number you can pick is: " + min);
        Debug.Log("Tell me if your number is higher or lower than: " + guess);


        max = max + 1;
    }




    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.UpArrow))
        {

            min = guess;
            NextGuess();
        }
        else if (Input.GetKeyDown(KeyCode.DownArrow))
        {
            max = guess;
            NextGuess();
        }
        else if (Input.GetKeyDown(KeyCode.Return))
        {
            Debug.Log("I am a genius");
            StartGame();
        }
    }
    void NextGuess()
    {
        guess = (max + min) / 2;
        Debug.Log("Is it higher or lower than..." + guess);
    }
}	![unity|586x325](upload://rbsDUJxMCuJoHjv6gQp9dknEXHx.jpeg) ```

It’s because when you press enter it calls StartGame which resets all your variables and plays the intro sequence. My suggestion is make a new function that checks to see if the guess is correct and gives you a message telling you if you’re correct or not.

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

Privacy & Terms