Hello World!
I’m probably getting too ahead of myself here but I tried declaring min/max as public variables so the player can set them in the scripcomponent within Unity.
The problem appears when 2 games are played in a row; min and max aren’t getting reset to their original values, rather they are keeping their most recent values from the previous game and I can’t figure out a way to reset them.
I also added a ‘# of tries’ counter as a test.
using UnityEngine;
using System.Collections;
public class NumberWizard : MonoBehaviour
{
public int max;
public int min;
int guess;
int tries;
void Start ()
{
StartGame ();
}
void StartGame ()
{
tries = 0;
guess = Random.Range(min,max);
//max += 1;
print ("Welcome to Number Wizard");
print ("Pick a number but don't tell me!");
print ("THe highest number you can pick is " + max);
print ("The lowest number is " + min);
print ("Is the number higher or lower than " + guess + "?");
}
void NextGuess ()
{
guess = (max + min) / 2;
print ("Higher or lower than " + guess);
tries += 1;
//print ("Tried #" + tries + "times");
}
void Update ()
{
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");
print ("PHEW! It took me #" + tries + " attempts this round");
print ("-------------------------------------------------------------");
StartGame();
}
}
}
I’m probably getting ahead by using public values bit I thought it shouldn’t be that hard… All I need is a way to reset them to the user defined values on StartGame but I can’t come up with the syntax (perhaps because I haven’t learnt it yet haha!).
Cheers,
Carl