So many errors!

Hey! My code has so many errors and Im only like a minute into the lecture. Please read over my code and establish what is wrong. Thanks in advance!

public class NumberWizard : MonoBehaviour {

	// Use this for initialization
	int max = 1000;
	int min = 1;
	int guess = 500;
	void Start () {
		StartGame();
	}
	void StartGame () {
		max = max + 1;
		
		print ("Welcome to Number Wizard");
		print ("Pick a munber 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 arrow = Higher / Down arrow = Lower / Enter = Perfect");
		
	
	}

	
	// 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 = (min + max) / 2;
		print ("Higher or lower than "   + guess);
		print ("Up arrow = Higher / Down arrow = Lower / Enter = Perfect");
	}
}
} else if (Input.GetKeyDown(KeyCode.DownArrow)) 

You’re missing an opening curly brace at the end of that line.

1 Like

Thank you so much! I was pulling my hair out over this to one curly bracket. Life saver!

1 Like

Hi Mocha, welcome to the community.

Your issue is a missing curly brace within the if statements inside your Update method.

One thing that would really help you is to format your brackets/braces in more of block form than trailing form.

For example;

void Start()
{
    // ....
}

instead of;

void Start() {

    // ....
}

Here is the problem;

	// 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();
		}
	}

Reformated, it would look like this;

// 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();
    }
}

Now if you look above, the missing curly brace stands out a little more obviously.

Thankyou for the example I will be sure to format this into my code. The quick responses on here are insane!

1 Like

You’re very welcome. @Martyn_Stewart and myself compete in Keyboard Wars :smiley:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms