2D Unity Course, Nested-If question

Hi, I’ve been having an issue with trying to make the nested if-statements in my update method work as intended . Totally new to C# but have a little experience with Python. I’ve tried all sorts of work arounds to no success and would appreciate something helping me with this little exercise.

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

public class NumberWizard : MonoBehaviour
{
    int minValue;
    int maxValue;
    int guess;
    string replay;
    
    void Start()
    {
        StartGame();
    }

    void StartGame()
    {
        minValue = 1;
        maxValue = 1000;
        guess = (1000 + 1) / 2;

        Debug.Log("Welcome to number wizard, let's have some fun !");
        Debug.Log($"Please pick a number between {minValue} and {maxValue}");
        Debug.Log($"Tell me if your number is higher or lower than my {guess}");
        Debug.Log("Push Up = Higher | Push Down = Lower | Push Enter = Correct");
        maxValue = maxValue + 1;
    }

    void NextGuess()
    {
        guess = (maxValue + minValue) / 2;
        Debug.Log($"Is your guess higher or lower than {guess} ?");
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            minValue = guess;
            NextGuess();
        }
        else if (Input.GetKeyDown(KeyCode.DownArrow))
        {
            maxValue = guess;
            NextGuess();
        }
        else if (Input.GetKeyDown(KeyCode.Return))
        {
            Debug.Log("Sweet, I'm a genius ! Want to play again ? (Y | N)");
            
            if (Input.GetKeyDown(KeyCode.Y))
            {
                StartGame();
            }
            else if (Input.GetKeyDown(KeyCode.N))
            {
                Debug.Log("Thanks for playing!");
            }
            //Debug.Log("Play again ? (Y | N)");
            //replay= System.Console.ReadLine();
            //if (replay == ("Y"))
            //{
            //    StartGame();
            //}
            //else if (replay == ("N"))
            //{
            //    Debug.Log("Thanks for playing mate");
            //}


        }

    }
}

Your nested loops are fine - the problem occurs because of the way that Unity is calling the Update function. “Update” gets called once per frame. So when you hit the up or down arrow and it asks you for a guess, that’s no problem - it simply calls that function.

However, in your code you’re asking it to see if “enter” is pressed, and then, if so, if “Y” or “N” is pressed. Well, when it goes to the next frame it won’t work because you’re no longer pressing return. Also, doing it this way means the player can continue to make guesses even when the game is over.

The simple fix is this:

Have a gameOver condition (a bool). Set it to false in the “StartGame” function. If game over is false, then run the whole update code. If you press “Return”, then ask if they want to start again. Then make game over true.

In your update function you can have an if statement at the end (outside of the “is it gameover” brackets) that checks if its game over. Here you can check your input, if they press Y start the game.

1 Like

Thanks for providing that explanation and solution, it’s something I’ll definitely keep in mind working with Unity especially going forward.

Here’s the working code, if it can help anyone else

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

public class NumberWizard : MonoBehaviour
{
    int minValue;
    int maxValue;
    int guess;
    bool gameOver;

    void Start()
    {
        StartGame();
    }

    void StartGame()
    {
        gameOver = false;
        minValue = 1;
        maxValue = 1000;
        guess = (1000 + 1) / 2;

        Debug.Log("Welcome to number wizard, let's have some fun !");
        Debug.Log($"Please pick a number between {minValue} and {maxValue}");
        Debug.Log($"Tell me if your number is higher or lower than my {guess}");
        Debug.Log("Push Up = Higher | Push Down = Lower | Push Enter = Correct");
        maxValue = maxValue + 1;
    }

    void NextGuess()
    {
        guess = (maxValue + minValue) / 2;
        Debug.Log($"Is your guess higher or lower than {guess} ?");
    }

    void Update()
    {
        if (gameOver == false)
        {
            if (Input.GetKeyDown(KeyCode.UpArrow))
            {
                minValue = guess;
                NextGuess();
            }
            else if (Input.GetKeyDown(KeyCode.DownArrow))
            {
                maxValue = guess;
                NextGuess();
            }
            else if (Input.GetKeyDown(KeyCode.Return))
            {
                Debug.Log("Sweet, I'm a genius ! Want to play again ? (Y | N) ");
                gameOver = true;
                Update();
                
            }
        }

        else if (gameOver == true)
        {
            if (Input.GetKeyDown(KeyCode.Y))
            {
                StartGame();
            }
            else if (Input.GetKeyDown(KeyCode.N))
            {
                Debug.Log("Thanks for playing!");
            }
        }
    }
}

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