Public min/max values causing problems

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

Hello Carl, how are you?

You can save the user defined values within two new variables, such as float userMin and float userMax, just declare them in the top of the script along with the other variables.
After that all you have to do is to save the value that the player has chosen to the min and to the max variables under the start method :

void Start()
{
userMin = min;
userMax = max;
StartGame();
}

and then reset the min and max value to userMin and userMax when the game is over:

else if (Input.GetKeyDown (KeyCode.Return)) 
	{
		print ("I won");
		print ("PHEW! It took me #" + tries + " attempts this round");
		print ("-------------------------------------------------------------");
		min = userMin;
		max = userMax:
		StartGame();
	}

Let me know if you managed to solve this.

Hey Joao, thanks a lot!

So the key was to create two separate sets of variables for the max/min values being the user-defined ones and the runtime ones respectively, then overwriting the runtime ones with the user-defined values again once gameplay starts over.
That makes sense! I’m completely new to programming so this has proven really helpful :slight_smile:
Cheers,
Carl

1 Like

You are welcome Carl!

Yes, storing more than one version of an item is a pretty frequent thing to be done, for example the health of a character, you will have to store the maximum, the current value and sometimes you will want to store the true health value (health without itens, modifiers and level ups), and even an health per level up value.

By the way, Is it working now?

Best Regards,

Johnny.

Like a charm, thanks!

1 Like

Privacy & Terms