I'm having some trouble challenging myself in Number Wizard

So I’ve been attempting to challenge myself and get ahead of the game on Number Wizard, I’m currently on #8 of Basic C# coding, Functions and Encapsulating.

In my code I’ve set Return to restart the game, up to that point everything works great. Space bar gives the first guess, up and down arrow keys trigger a higher or lower guess, but once the code restarts via Return, I can use the up and down arrow keys to trigger more guess, but I can not start a third game.

I read some other questions under the video, and saw some mention of game states, is that the issue here? How would this be applied if so?

Small edit: I noticed the second game will base the min/max on the previous answer. I believe the solution to this was supposed to be setting the public class min/max to simply int Min/Max, and applying the min/max/guess into the StartGame/NewGame voids. It seems to not reset the number so I’m wondering if someone can correct me there. If all else fails I’ll scratch what I’ve done different from the videos and just try to finish out the lectures of this subject.

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

public class NumberWizard : MonoBehaviour
{
    int MinChoice;
    int MaxChoice;
    int Guess;

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

    void StartGame()
    {
        MinChoice = 1;
        MaxChoice = 1000;
        Guess = 500;
        Debug.Log("Welcome to number wizard! Let me quickly explain the rules. I'd like for you to pick a number between 1 and 1000. Once you've done it, I will give you my guess!");
        Debug.Log("Press Up if it's higher, press Down if it's lower, and press Enter if it's correct.");
        Debug.Log("Please press the spacebar when you would like to start the game.");
        MaxChoice = MaxChoice + 1;
    }
    // Attempting to create void NewGame to have a different text from the beginning game, Return key does not restart the game more than once. A similar issue comes up if I'm using Spacebar to continue a second game when using void StartGame under Return instead of NewGame.
    void NewGame()
    {
        MinChoice = 1;
        MaxChoice = 1000;
        Guess = 500;
        Debug.Log("Welcome back! I believe you're familiar with the rules, but if you're not I'll quickly cover them again.");
        Debug.Log("Press Up if it's higher, press Down if it's lower, and press Enter if it's correct.");
        Debug.Log("Alright, here we go! I'm going to guess your number is: " + Guess + " Am I correct?");
        MaxChoice = MaxChoice + 1;
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            Debug.Log("Alright, here we go! I'm going to guess your number is: " + Guess + " Am I correct?");
        }

        else if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            MinChoice = Guess;
            Guess = (MaxChoice + MinChoice) / 2;
            Debug.Log("Looks like it's higher, I'll guess again. is this correct? " + Guess);
        }

        else if (Input.GetKeyDown(KeyCode.DownArrow))
        {
            MaxChoice = Guess;
            Guess = (MaxChoice + MinChoice) / 2;
            Debug.Log("Looks like it's lower, I'll guess again. is this correct? " + Guess);
        }
        
        else if (Input.GetKeyDown(KeyCode.Return))
        {
            Debug.Log("Aha! I knew I'd get there eventually! I will be restarting the game now, I will re-familiarize you with the rules.");
            NewGame();
        }

    }
}

If there are any questions / issues with clarity, please let me know and I’ll explain myself the best I can.

Hi,

Good job on challenging yourself. :slight_smile:

I have to admit that I do not see any issue in your code. The NewGame method resets the variables to the initial values. And those values are hard-coded and do not depend on any previous values.

Is “Collapse” enabled in your console?

Well that’s embarrassing; turns out I wasn’t noticing the numbers rising on Collapsed logs. Turned it off, everything ran perfectly. Sometimes it’s just the simplest answers I suppose. Thanks for your help!

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

Privacy & Terms