NumberWizard game works, except it does not loop?!?

Hello,
So for all I know there is something stupid simple that i may be overlooking, my game works. I have gone back and(at least with my one set of eyes) checked over the code in each video, up until the end of functions and encapsulation, as still my game will run without error and is perfect upon the first run, once the game ends and the number has been marked as correct with the return key, the game stays at the same guess number and does not reset? I am getting frustrated by this… but the urge to fix it and learn is stronger still… please help? my code is below…

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

public class NumberWizard : MonoBehaviour

{
    int max;
    int min;
    int guess;


    // Use this for initialization
    void Start()
    {
        StartGame();
    }

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

        Debug.Log("G'day mate, welcome to the numba wiz-ud...");
        Debug.Log("Please pick a number, don't tell me what it is...");
        Debug.Log("The highest number is: " + max);
        Debug.Log("The lowest number is: " + min);
        Debug.Log("Tell me if your number is higher or lower than: " + guess);
        Debug.Log("Push Up = Higher, Push Down = Lower, Push Enter = Correct");
        max = max + 1;
    }
    // Update is called once per frame
    void Update()
    {

        // Detect when the up arrow key is pressed down
        if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            min = guess;
            NextGuess();
        }

        // Detect when the down arrow key is pressed down
        else if (Input.GetKeyDown(KeyCode.DownArrow))
        {
            max = guess;
            NextGuess();
        }

        // Detect when the return key is pressed down
        else if (Input.GetKeyDown(KeyCode.Return))
        { 
            Debug.Log("I am a genius!");
        }
    }
    void NextGuess()
    {
        guess = (max + min) / 2;
        Debug.Log("Is it higher or lower than..." + guess);
    }
}

Hi Christopher,

So if I understand you correctly, you want the game to restart once the number is found and the enter key is pressed. It seems to me then that there is something missing from this part:

else if (Input.GetKeyDown(KeyCode.Return))
{ 
    Debug.Log("I am a genius!");
}

A function call perhaps? :wink:

yes, that makes sense… though forgive my newbie’ness. But if that were the case, and since my code is identical to the instructors, even in this spot… i.e. https://www.udemy.com/unitycourse/learn/v4/t/lecture/10522514?start=0 (functions and encapusating, 6:05)
would he have to do the same? and why does his code work the way it is intended yet mine does not without said function?

I don’t see his game restarting before he adds said function call :slight_smile: In fact he has this exact problem at 8:10 in the video

oh my gawd… i’m such a newbie… hahaha Thank you!! bless you!!
I about fell out of my chair for my own derpiness… Word’s really don’t express. I truly appreciate you!

Haha, no problem my friend, glad I was able to help :wink:

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

Privacy & Terms