Problem with Code for Number Wizard

Whenever I try to run the code in Unity it runs totally fin until the guess is correct until it is time to restart. Once the guess is correct the entire program freezes.

Note: So far I’ve tried moving the reset to a different function, changing the keys for the correct answer, along with a few other short attempts that I’ve probably forgotten. Theirs a lot of code commented out from things I was trying to do to debug. Please let me know what you think.

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

public class NumberWizard : MonoBehaviour
{
int maxGuess;
int minGuess;
int guess;
int replay;
// Start is called before the first frame update
void Start()
{

        StartGame();
    

}

void StartGame()
{
    //Debug.Log("Test A");
    maxGuess = 1000;
    minGuess = 1;
    guess = 500;

    Debug.Log("Welcome to number Wizard.");
    Debug.Log("Choose a number. Don't tell me what it is.");
    Debug.Log("The highest option is : " + maxGuess);
    Debug.Log("The lowest option is : " + minGuess);
    Debug.Log("My guess is " + guess);
    Debug.Log("Push up for greater.Push down for less");
    Debug.Log("push enter if it is correct");
    //Debug.Log("Test B");
    Update();


}

// Update is called once per frame
void Update()
{
    if (Input.GetKeyDown(KeyCode.UpArrow))
    {
        Debug.Log("It is higher than " + guess + ".");
        minGuess = guess;
        //Debug.Log("Test C");
        NextGuess();

    }
    else if (Input.GetKeyDown(KeyCode.DownArrow))
    {
        Debug.Log("It is lower than " + guess + ".");
        maxGuess = guess;
        //Debug.Log("Test D");
        NextGuess();
    }
    else if (Input.GetKeyDown(KeyCode.Return))
    {
        //Debug.Log("Test G");
        Debug.Log("I knew it!");
        //Debug.Log("Test E");
        /////////////////////////////
        //StartGame();///////////////
        /////////////////////////////
        ///
        StartGame();

    }

    void NextGuess()
    {
        guess = (maxGuess + minGuess) / 2;
        Debug.Log("Is it higher or lower than " + guess);

    }
    /*void restart()
    {
        maxGuess = 1000;
        minGuess = 1;
        guess = 500;
        Debug.Log("Choose a number. Don't tell me what it is.");
        Debug.Log("The highest option is : " + maxGuess);
        Debug.Log("The lowest option is : " + minGuess);
        Debug.Log("My guess is " + guess);
        Debug.Log("Push up for greater.Push down for less");
        Debug.Log("push enter if it is correct");

        Update();
        Debug.Log("No bugs yet");
    }*/
}

}

Hi,

Welcome back to our community! :slight_smile:

Before you start looking for the problem in your code, make sure that “Collapse” is not enabled in your console.

Then make sure that there are no compiler errors. In your code, the restart() method seems to be commented out. And it does not seem to get called anywhere.

None of the code is collapsed and the function restart() was the same code from StartGame() when I was trying to see if the problem was calling StartGame() within the same function.

This is the console using the number 625 as the example. Both parts of the Update() function are working but once I get to the second else if it freezes up. It is able to reach the text I knew it if that is all it calls but when I add StartGame() anywhere in that loop it freezes and stops working, even if “I knew it” proceeds it.

“Collapse” is enabled in the Console. Please disable it. The numbers next to the messages show how often the message appeared. When you “restart” your game, the first message does not appear below the last message but at the top of your console.

image