Need help, been trying to fix this for hours? Keep getting errors

using UnityEngine;
using System.Collections;

public class NumberWizard : MonoBehaviour {
// Use this for initialization
int max;
int min;
int guess;

void Start () {
	StartGame();	
}

void StartGame () {
	int max = 1000;
	int min = 1;
	int guess = 500;
	
	max = max +1;
	
	print ("Welcome to NumberWizard");
	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, return = equal");
}
		
// Update is called once per frame
void Update () {
	if (Input.GetKeyDown(KeyCode.UpArrow))	{
		min = guess;
		NextGuess();
	} else	if (Input.GetKeyDown(KeyCode.DownArrow)) {
		max = guess;
		NextGuess();
	} else	if (Input.GetKeyDown(KeyCode.Return)){
		print("I won!");
		StartGame();
	}
}

void NextGuess () {
	guess = (max + min) / 2;
	print ("higher or lower than " + guess);
	print ("Up = higher, down = lower, return = equal");
}

}

You are declaring int, max and guess inside StartGame(), you should be declaring it only outside it and before the start method. Take out the keyword int of those three variables within the startgame and it should work

1 Like

Thank you so much for your help! I’m in work now so I’m going to attack it again with your advice when I’m home! Thanks again

1 Like

Yeah, it should look something like this based on your code structure.

int max = 1000;
int min = 1;
int guess = 500;

void Start () {
	StartGame();	
}

void StartGame () {
max = max + 1;

print ("Welcome to NumberWizard");
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, return = equal");

Privacy & Terms