Enter restarts the game only once. Int don't reset after Enter

Hi all,

Game starts with no errors. I can reach 1000 and 1 by pressing up or down continuously.
However, if I press enter, the game will seemingly restart in the console but only visually. It will not function properly:

  1. Pressing the enter key will no longer display the “I won!” print, neither will it reset the game again.
  2. If I were to press up or down, the new results will be based on the prior game session (wont start from 500 but rather from the last answer on which enter was pressed).
  3. The print with the instruction (which should appear after every input of up or down will only appear after the 1first selection of each key and then stops so that the first time I press up I get:
    Higher or lower than 750
    Up = higher, down = lower, return = equal
    But the second time will show only:
    Higher or lower than 875
  4. After pressing enter for the first time, the first few presses of the arrow buttons may go ignored before the game continues to present the Guess that was saved from the previous game. For instance, After starting the game I press down arrow which displays:
    Higher or lower than 250
    Up = higher, down = lower, return = equal
    Pressing the down arrow again results only in:
    Higher or lower than 125
    If I then press enter, I get "I won and the start instructions. But If I then press down again nothing happens. This persists for 2 presses of the down arrow. On the third I get:
    Higher or lower than 63

I’ve verified the code line by line and can’t find errors (of course I’m probably missing something).

My Code:

using UnityEngine;
using System.Collections;

public class NumberWizard : MonoBehaviour {
	
	// Use this for initialization
	
	int max;
	int min;
	int guess;
	
	void Start () {
		StartGame ();
		
	}
	
	void StartGame (){
		
		max = 1000;
		min = 1;
		guess = 500;
		
		max = max +1;
		
		print ("==========================");
		print ("welcome to Number Wizard");
		print ("Pick a number in your head, but don't tell me!");		
		
		print ("The highest number you can pick is " + max);
		print ("The lowest number you can pick is "    + min);
		
		print ("Is the number higher or lower than " + guess);
		print ("Up arrow = higher, Down arrow = lower, return = equal");
		
	}
	
	// Update is called once per frame
	void Update () {
		
		if (Input.GetKeyDown(KeyCode.UpArrow)) {
			// print ("Up arrow pressed");
			min = guess;
			NextGuess();
			
		} else if (Input.GetKeyDown(KeyCode.DownArrow)) {
			// print ("Down arrow pressed");
			max = guess;
			NextGuess();
			
		} else if (Input.GetKeyDown(KeyCode.Return)) {
			print("I won!");
			StartGame ();
		}	
	}
	
	void NextGuess () {
		guess = (max + min) / 2;
		print ("Higher or lower than " + guess);
		print ("Up = higher, down = lower, return = equal");
	}
		
}

Please help :slight_smile:

Hello Sheer,

Could you run your game and then run through those reproduction steps and take a screenshot of the Unity editor with the console clearly shown please. You can just paste the image in to a reply here.

Nice write up for the reproduction steps by the way :slight_smile:

Privacy & Terms