[Help] Ridding Compiler Errors

Hello,

I have a recurring issue with lesson 19 (using IF) in this session. I was attempting to add the KeyCode.Return aspect of lesson 18 and I keep getting a parsing error.

Foolishly i started again and the error persists.

I am able to get to this point error free -

but then once I enter the following code

image//

Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.UpArrow)) {
print (“Up arrow pressed”);
}

    if (Input.GetKeyDown(KeyCode.DownArrow)) {
        print ("Down arrow pressed");
    }
    
    if (Input.GetKeyDown(KeyCode.Return)) {
        print ("I won");
    }
}

Once i press play - i then get this image

I get a parsing error, than then tells me I have a complier error that I cannot get rid of. I know compiler error questions have been asked before but I cannot seem to find one dealing with these specifics.

Any assistance for this newby would be greatly appreciated.

Paul

Hi Paul! You are missing some closing curly braces: one to close the Update() function, and one to close the class. :slight_smile:

Hi Sebastian, thanks for the lightning reply, really sorry but I am not sure I follow - I have it as void Update () {

and also not sure on class?

sorry

its all a bit moot at the moment though as I cannot rid the Unity Screen of the compiler error -

sorry for being dense

No problem :slight_smile: The class is NumberWizard, declared at the top of the file in

public class NumberWizard : Monobehaviour {

You need a curly brace at the end of the file to complement that opening curly brace and close the class.
And don’t worry, you’re not dense, we all have to learn these things :wink:

Edit: the indentation (number of spaces at the start of the lines) can help you figure out what belongs to what function. The } on line 34 is on the same level as the if statement, therefore it closes that. The one on line 35 is on the same level as the Update() function, therefore it closes that. You need one on the same level as the class declaration as well.

2 Likes

HI Sebastian, thank you for that, have done it and it works fine. The indentation number is very handy - thanks again!

Phew my morning wasnt wasted :slight_smile:

1 Like

Glad to hear it :wink:

1 Like

Nothing to apologise for Paul and Sebastian is spot on… these curly braces, brackets and semi-colons can catch us all out.

You may find, to start with at least, that by slightly reformatting the layout of your braces and brackets they are easier to see.

For example, instead of;

public class NumberWizard : MonoBehaviour {

    void Start() {

    }

    void Update() {

    }
}

You could lay them out this way;

public class NumberWizard : MonoBehaviour
{
    void Start()
    {

    }

    Update()
    {

    }
}

You may find that your eye will then look for the corresponding closing brace/bracket with the opening one in a vertical line above it. You would, of course, need to apply the same formatting to any code within the methods, such as an if statement;

public class NumberWizard : MonoBehaviour
{
    private bool awesomeness = true;

    void Start()
    {
        if (awesomeness == true)
        {
            Debug.Log("Paul and Sebastian are awesome!");
        }
    }

    Update()
    {

    }
}

I’m not sure whether MonoDevelop offers it as a feature, but Visual Studio (the community edition is free) will highlight the corresponding brace/bracket when you hover over one.

image

Note, with regards to code, you can paste it directly into the forum and apply code formatting, this is often preferential to screenshots as it allows those who are offering help to copy/paste your code back into their reply with corrections. Screenshots are very useful however for things like the details within the Hierarchy or Inspector when it comes to Unity.

Hope this is of use.


See also;

3 Likes

Hi Rob, of great use, many thanks. I like the positioning of the brackets you have above, will use that from now, cheers!

1 Like

You’re more than welcome Paul.

There is no real hard and fast way when it comes to the layout of them, you will find, naturally, different developers favour different approaches. As such, it is handy to be able to spot them in either of these layouts as you may find examples online, or those which people offer you here, are the opposite of that which you prefer :slight_smile:

I can’t tell you how many times I’ve fallen victim to this as well.

That is a very insightful way of formatting the curly braces Rob. Thanks for that!

2 Likes

You are very welcome Tim :slight_smile:

Privacy & Terms