Why won't this function work?

I’ve tried slightly changing the code for number wizard while including parameters in my NextGuess function. But for some reason, using this function causes the game to not work (stuck after the second guess). Keep in mind that the same code without the use of this function worked fine. What is the problem?

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

public class NumberWizard : MonoBehaviour
{
    // Start is called before the first frame update
    int max = 1000;
    int min = 1;
    int guess = 500;
    void Start()
    {
        StartGame();
    }

    void StartGame()
    {
        Debug.Log("Welcome to Number Wizard! Thanks for playing!");
        Debug.Log("Pick a number: ");
        Debug.Log("Highest number you can pick is: " + max);
        Debug.Log("Lowest number you can pick is: " + min);
        Debug.Log("Tell me if your number is higher or lower than " + guess);
        Debug.Log("Press up arrow = Higher, press down arrow = Lower, press Enter = Correct.");
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            min = guess + 1;
            if ((min + max) / 2 == guess)
            {
                Debug.Log("Your number has to be " + guess);
            } 
            else
            {
                NextGuess("higher", "lower", guess, min, max);
            }
        }

        else if (Input.GetKeyDown(KeyCode.DownArrow))
        {
            max = guess - 1;
            if ((min + max + 1) / 2 == guess)
            {
                Debug.Log("Your number has to be " + guess);
            }
            else
            {
                NextGuess("lower", "higher", guess, min, max);
            }
        }

        else if (Input.GetKeyDown(KeyCode.Return))
        {
            Debug.Log("I found your number. It is " + guess);
        }
    }
    void NextGuess(string string1, string string2, int guess, int min, int max)
    {
        Debug.Log("You told me that the number was " + string1 + " than " + guess + ". It should still be " + string2 + " than " + min);
        guess = (min + max) / 2;
        Debug.Log("New guess is " + guess);
    }

Hi,

Welcome to our community! :slight_smile:

I’m sorry that you didn’t get an answer earlier. Have you already figured out what the problem is/was in your code?

Please a break point on your code, and click Attach to Unity. What happens on the second time your function is called when you step through it with the debugger?

I think it’s your variable scope, you got two variables named guess one that belongs to the class instance and one that belongs to the NextGuess function. It looks like you only changed the NextGuess guess so once the function is over it doesn’t remember. To change the class instance guess you will need to refer to it with the “this” keyword. this.guess = (min + max) / 2; or rename the function guess.

I hope that makes sense and is helpful!

1 Like

You are reinitalising your variables every time. Use different names for the variables. Guess1…etc. would work.

This topic was automatically closed after 14 days. New replies are no longer allowed.

Privacy & Terms