Hello everyone, I have no idea how we’re supposed to let the user assign the values in a convenient fashion. It seems… awkward to say the least. I spent hours looking for a solution but nothing seemed easy to use. But, I figured I’d give it a try anyway by allowing players to incrementally alter the minimum and maximum values. I’ve run into trouble though, for whatever reason my game won’t even start when I try to use the NUMPAD+ and NUMPAD- buttons to alter the values, could someone help me understand why?
It’s in the SetValues() function, at least that’s where the code that’s supposed to run is. Click the spoiler below to look at the code.
[spoiler]using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NumberWizard : MonoBehaviour {
//Do the rubber duck
// Use this for initialization
int max;
int min;
int guess;
int playing;
int setting;
void Start ()
{
SetValues();
}
void StartGame()
{
playing = 1;
setting = 0;
print("==========================");
print ("Welcome to Number Wizard");
print ("First, decide on a range of numbers. It can be any range you want, but only whole numbers.");
print ("Type the numbers for your minimum value here: ");
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 = higher, DOWN = lower, and RETURN = equal.");
max = max +1;
}
//PLAYING
void NextGuess () {
guess = (max + min) / 2;
print ("Higher or lower than " + guess);
print ("UP = higher, DOWN = lower, and RETURN = equal.");
}
//SETTING THE MINIMUM AND MAXIMUM VALUES BEFORE PLAYING
void SetValues ()
{
playing = 0;
setting = 1;
print ("Let's set your minimum and maximum value. To start, press NUMPAD+ to set your Maximum Value or NUMPAD- to set your minimum value. If you want to start the game, press SPACE.");
if (Input.GetKeyDown (KeyCode.KeypadMinus)) {
SetMin();
} else if (Input.GetKeyDown (KeyCode.KeypadPlus)) {
SetMax();
} else if (Input.GetKeyDown (KeyCode.Space)) {
StartGame ();
}
}
//SET MINIMUM
void SetMin ()
{
while (setting == 1) {
print ("Press and hold the UP and DOWN arrowkeys to set your values, press NUMPAD+ to set the maximum value. Press SPACE to start the game.");
if (Input.GetKeyDown (KeyCode.DownArrow)) {
min--;
print ("Current Minimum Value: " + (min));
} else if (Input.GetKeyDown (KeyCode.UpArrow)) {
min++;
print ("Current Minimum Value: " + (min));
} else if (Input.GetKeyDown (KeyCode.KeypadPlus)) {
print ("Final Minimum Value: " + (min));
SetMax ();
} else if (Input.GetKeyDown (KeyCode.Space)) {
StartGame ();
}
}
}
//SET MAXIMUM
void SetMax ()
{
while (setting == 1) {
print ("Press and hold the UP and DOWN arrowkeys to set your values, press NUMPAD+ to set the maximum value. Press SPACE to start the game.");
print ("Next, let's set your maximum value.");
if (Input.GetKeyDown (KeyCode.DownArrow)) {
max--;
print ("Current Maximum Value: " + (max));
} else if (Input.GetKeyDown (KeyCode.UpArrow)) {
max++;
print ("Current Maximum Value: " + (max));
} else if (Input.GetKeyDown (KeyCode.KeypadMinus)) {
SetMin ();
} else if (Input.GetKeyDown (KeyCode.Space)) {
StartGame ();
}
}
}
// Update is called once per frame
void Update ()
{
while (playing == 1){
if (Input.GetKeyDown (KeyCode.UpArrow)) {
//print ("Up key was pressed.");
min = guess;
NextGuess();
} else if (Input.GetKeyDown (KeyCode.DownArrow)) {
//print ("Down key was pressed.");
max = guess;
NextGuess();
} else if (Input.GetKeyDown (KeyCode.Return)) {
print ("I won!");
StartGame();
}
}
}
}
//below code used to be in StartGame()
/guess = 500;
max = 1000;
min = 1;/[/spoiler]