why when i type } in visual studio C# it matches with a different { basically it looks like this:

Dylan Coetzee 1 Reputation point
2021-01-31T02:43:34.617+00:00

62209-image.png

so my question is why is the first { highlighted with the } at the end of if (movemnt > 0.05) {anim.SetBool("isRunning", true); {anim.SetBool("isRunning", true)

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,111 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Alberto Poblacion 1,561 Reputation points
    2021-01-31T13:30:07.143+00:00

    The way in which the highlighting works is that Visual Studio uses the language parser to analyze your code, and then selects the brace that matches the opening one, in such a way that they both enclose the same "logical block". This means that if you select the { at the beginning of an "if", it will highlight the } that closes the "if" block in accordance with the compiler. This may not necessarily be the first } after the selected one, because the "if" block might have other nested blocks that contain their own opening and closing braces.

    Note that Visual Studio can get very confused when there are any syntax errors within the code. Then the parser will not be able to analyze it properly and therefore will not be able to find proper matches for the braces. In the particular example that you showed, there are syntax errors because the braces are unmatched (superfluous brace before the second anim.SetBool). Therefore, it is not unexpected for Visual Studio to fail to match them in the way that you think they should be matched.

    1 person found this answer helpful.
    0 comments No comments

  2. Timon Yang-MSFT 9,591 Reputation points
    2021-02-01T03:27:05.013+00:00

    I saw a few problems.

    1. You call the SetBool() method on a float number. Should it be called in _rigidBody: rigidBody.SetBool();
    2. You call the SetBool() method twice, they are exactly the same, do you need to do this in Unity or just accidentally wrong? I think calling it once should be enough.
    3. The if statement is now outside the method body, place it in the correct method.

    I don't know much about Unity. If some features of Unity cause my understanding to be wrong, please let me know.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.