Search Unity

Cant get keybinding to work

Discussion in 'Scripting' started by crowysight, Dec 26, 2016.

  1. crowysight

    crowysight

    Joined:
    Dec 26, 2016
    Posts:
    2
    Hello I am making a game where you chose a range of number then think of a number and then the computer figures out the number you are thinking of. However when I was trying to make It so you could chose the range of numbers I tested It and the keybinding isn't working and I have tried quite a bit of stuff to make It work but to no avail so some help would be nice.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Numblywizard : MonoBehaviour {
    5.  
    6.     // Use this for initialization
    7.     int max;
    8.     int min;
    9.     int guess;
    10.    
    11.     void Start () {
    12.         StartGame();
    13.     }
    14.    
    15.     void StartGame () {
    16.         min = 1;
    17.         max = 500;
    18.         guess = Random.Range(min,max);
    19.        
    20.         print ("==========================");
    21.         print ("Welcome to Number Wizard!");
    22.         print ("First you must chose the range in which you want me to pick a number");
    23.         print ("Press 1 for 1-100, 2 for 100-1000, 3 for 1-10000, 4 for 1000-10000, 5 for 1-100000, 6 for 10000-100000,7 for 1-1000000, and 8 for 100000-1000000");
    24.        
    25.         if (Input.GetKeyDown(KeyCode.Alpha1)) {
    26.             max = 1000;
    27.             min = 1;
    28.             max = max + 1;
    29.             FirstGuess();
    30.         }else if (Input.GetKeyDown(KeyCode.Alpha2)) {
    31.             max = 1000;
    32.             min = 100;
    33.             max = max + 1;
    34.             FirstGuess();
    35.         }else if (Input.GetKeyDown(KeyCode.Alpha3)) {
    36.             max = 10000;
    37.             min = 1;
    38.             max = max + 1;
    39.             FirstGuess();
    40.         }else if (Input.GetKeyDown(KeyCode.Alpha4)) {
    41.             max = 10000;
    42.             min = 1000;
    43.             max = max + 1;
    44.             FirstGuess();
    45.         }else if (Input.GetKeyDown(KeyCode.Alpha5)) {
    46.             max = 100000;
    47.             min = 1;
    48.             max = max + 1;
    49.             FirstGuess();
    50.         }else if (Input.GetKeyDown(KeyCode.Alpha6)) {
    51.             max = 100000;
    52.             min = 10000;
    53.             max = max + 1;
    54.             FirstGuess();
    55.         }else if (Input.GetKeyDown(KeyCode.Alpha7)) {
    56.             max = 1000000;
    57.             min = 1;
    58.             max = max + 1;
    59.             FirstGuess();
    60.         }else if (Input.GetKeyDown(KeyCode.Alpha8)) {
    61.             max = 1000000;
    62.             min = 100000;
    63.             max = max + 1;
    64.             FirstGuess();
    65.         }      
    66.     }
    67.  
    68.     void FirstGuess() {
    69.         print ("The highest number you can pick is " + max);
    70.         print ("The lowest number you can pick is " + min);
    71.    
    72.         print ("Is the number higher or lower than " + guess);
    73.         print ("Up arrow = higher, down arrow = lower, Enter = When I have chosen the correct number");
    74.     }
    75.    
    76.     // Update is called once per frame
    77.     void Update () {
    78.         if (Input.GetKeyDown(KeyCode.UpArrow)) {
    79.             min = guess;
    80.             NextGuess();
    81.         }else if (Input.GetKeyDown(KeyCode.DownArrow)) {
    82.             max = guess;
    83.             NextGuess();
    84.         }else if (Input.GetKeyDown(KeyCode.Return)) {
    85.             print("I won!");
    86.             StartGame();
    87.         }
    88.     }
    89.    
    90.     void NextGuess () {
    91.         guess = Random.Range(min,max);
    92.         print ("Higher or lower than " + guess);
    93.         print ("Up arrow = higher, down arrow = lower, Enter = When I have chosen the correct number");
    94.         Update();
    95.     }
    96. }
     

    Attached Files:

    Last edited: Dec 27, 2016
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,175
unityunity