Problems in my Number Wizard code

Howdy, I’ve just started following the course but I’ve hit two problems.

  • My keyboard input is still reading the up, down, enter keys all at one if pressed all at once.

  • My ‘Guess’ numbers are not scaling higher or lower, the up and down arrow keys just result in ‘guess’ to pop up.

Here is my code so far. Any advice would be loved.

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

public class NumberWizard : MonoBehaviour
{
int max = 1000;
int min = 1;
int guess = 500;

// Start is called before the first frame update
void Start()
{
    
    Debug.Log("Welcome to number wizard, yo");
    Debug.Log("Choose a number Mortal");
    Debug.Log("The highest you may pick is: " + max);
    Debug.Log("The lowest is: " + min);
    Debug.Log("Tell me if your number is higher or lower than 500");
    Debug.Log("Push Up = Higher,  Push Down = Lower,  Enter = Correct");
    max = max + 1;
}

// Update is called once per frame
void Update()
{
    if (Input.GetKeyDown(KeyCode.UpArrow))
    { 
    Debug.Log("Up Arrow key was pressed.");
        min = guess;
        guess = (max + min) / 2;
        Debug.Log("guess");
}
    
        //Detect when the down arrow key is pressed down
        else if (Input.GetKeyDown(KeyCode.DownArrow))
    { 
            Debug.Log("Down Arrow key was pressed.");
        max = guess;
        guess = (max + min) / 2;
        Debug.Log("guess");

; }
//Detect when the Return key is pressed down
else if (Input.GetKeyDown(KeyCode.Return))
{
Debug.Log(“Return key was pressed.”);
}
}
}

Hey! I just started as well, and can answer one of your questions… and actually ran into the SAME issue with the keys pressing together, but I haven’t figured out that one. If you find out, please let me know!

So, with your second issue and it printing the word “guess” instead of the value, it’s actually easy fix. We’ll walk through it, cause it’s good for both of us. =] So the line:

Debug.Log(“guess”)

is what is displaying the text, correct? Functioning correctly, but putting out the wrong thing. The reason is because of the quotes you have around “guess”. As soon as you put quotes around it, it becomes a string and is printed exactly as is.

If you remove the quotes, it then looks at it as the object named guess, goes and finds what that value is, and then prints that. Make sense? Just delete those quotes and you should be golden.

Now we just have to figure out why our “else if” commands aren’t preventing simultaneous button presses…

Ow, you are printing a string “guess” not the int guess values, remove the double quotes.

Sorry for the late response! Thank you so much for the help! :smiley:
If I figure out the problem with the “else if” command I’ll be sure to let you know.

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

Privacy & Terms