(Solved): When I press up or down I get 6 guesses

So I am following along and when I press up or down instead of the program making one guess, it executes the guess code six times.

My code:

void Update () {
		if (Input.GetKey(KeyCode.UpArrow)){
			min = guess;
			guess = (max + min) / 2;
			print ("Higher or lower than " + guess + "?");
		}else if (Input.GetKey(KeyCode.DownArrow)){
			max = guess;
			guess = (max + min) / 2;
			print ("Higher or lower than " + guess + "?");
		}else if (Input.GetKey(KeyCode.Return)){
			print("I Won!");
		}
	
	}

Results from console log after pressing the arrow key once:

Is the number higher or lower than 500?
UnityEngine.Debug:Internal_Log(Int32, String, Object)
UnityEngine.Debug:Log(Object)
UnityEngine.MonoBehaviour:print(Object) (at C:\buildslave\unity\build\artifacts\EditorGenerated\UnityEngineMonoBehaviour.cs:108)
NumberWizard:Start() (at Assets\Scripts\NumberWizard.cs:19)
 
(Filename: Assets/Scripts/NumberWizard.cs Line: 19)

Up arrow for higher, down arrow for lower, return for equal
UnityEngine.Debug:Internal_Log(Int32, String, Object)
UnityEngine.Debug:Log(Object)
UnityEngine.MonoBehaviour:print(Object) (at C:\buildslave\unity\build\artifacts\EditorGenerated\UnityEngineMonoBehaviour.cs:108)
NumberWizard:Start() (at Assets\Scripts\NumberWizard.cs:20)
 
(Filename: Assets/Scripts/NumberWizard.cs Line: 20)

Higher or lower than 750?
UnityEngine.Debug:Internal_Log(Int32, String, Object)
UnityEngine.Debug:Log(Object)
UnityEngine.MonoBehaviour:print(Object) (at C:\buildslave\unity\build\artifacts\EditorGenerated\UnityEngineMonoBehaviour.cs:108)
NumberWizard:Update() (at Assets\Scripts\NumberWizard.cs:29)
 
(Filename: Assets/Scripts/NumberWizard.cs Line: 29)

Higher or lower than 875?
UnityEngine.Debug:Internal_Log(Int32, String, Object)
UnityEngine.Debug:Log(Object)
UnityEngine.MonoBehaviour:print(Object) (at C:\buildslave\unity\build\artifacts\EditorGenerated\UnityEngineMonoBehaviour.cs:108)
NumberWizard:Update() (at Assets\Scripts\NumberWizard.cs:29)
 
(Filename: Assets/Scripts/NumberWizard.cs Line: 29)

Higher or lower than 937?
UnityEngine.Debug:Internal_Log(Int32, String, Object)
UnityEngine.Debug:Log(Object)
UnityEngine.MonoBehaviour:print(Object) (at C:\buildslave\unity\build\artifacts\EditorGenerated\UnityEngineMonoBehaviour.cs:108)
NumberWizard:Update() (at Assets\Scripts\NumberWizard.cs:29)
 
(Filename: Assets/Scripts/NumberWizard.cs Line: 29)

Higher or lower than 968?
UnityEngine.Debug:Internal_Log(Int32, String, Object)
UnityEngine.Debug:Log(Object)
UnityEngine.MonoBehaviour:print(Object) (at C:\buildslave\unity\build\artifacts\EditorGenerated\UnityEngineMonoBehaviour.cs:108)
NumberWizard:Update() (at Assets\Scripts\NumberWizard.cs:29)
 
(Filename: Assets/Scripts/NumberWizard.cs Line: 29)

Higher or lower than 984?
UnityEngine.Debug:Internal_Log(Int32, String, Object)
UnityEngine.Debug:Log(Object)
UnityEngine.MonoBehaviour:print(Object) (at C:\buildslave\unity\build\artifacts\EditorGenerated\UnityEngineMonoBehaviour.cs:108)
NumberWizard:Update() (at Assets\Scripts\NumberWizard.cs:29)
 
(Filename: Assets/Scripts/NumberWizard.cs Line: 29)

Higher or lower than 992?
UnityEngine.Debug:Internal_Log(Int32, String, Object)
UnityEngine.Debug:Log(Object)
UnityEngine.MonoBehaviour:print(Object) (at C:\buildslave\unity\build\artifacts\EditorGenerated\UnityEngineMonoBehaviour.cs:108)
NumberWizard:Update() (at Assets\Scripts\NumberWizard.cs:29)
1 Like

ok so I figured it out a minute after posting. Sorry.

The problem is I needed to use Input.GetKeyDown() and not GetKey()

2 Likes

Isn’t that always the way! :slight_smile:

It’s not a problem, and often these posts serve wonderfully to help other people, as you have outlined what the issue was - thank you :slight_smile:

Privacy & Terms