I can't Find Errors In My Code!

Hi. I have made some simple C# code to try Unity out, attached the .cs file to a GameObject, pressed the play button, and all it gives me is “All compiler errors have to be fixed before you can enter playmode!”. I looked at the console and there is a list of about 6 Compile-Time errors there. I have checked and checked AND checked my code, but it still gives me several Compile-Time errors. Errors and C# code is as follows:

public class Controll : MonoBehaviour
{
  bool UpPresedQ;
  void Update()
  {
    UpPresedQ = (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow));
    if (UpPresedQ)
    {
      Debug.Log("W or Up Arrow is being pressed");
    }
  }
}

The way you use the key word “set” does not make sense in your code. I think you’re misunderstanding what it’s used for. I think your code will work as you expect if you delete the word “set” and the curly braces {} that immediately follow.

for example, I think instead of

set { upresedQ = false; }

what you really want is

 upresedQ = false;

You should pay close attention to the error messages themselves. They’re cryptic at first, but as you get to better understand what’s going on you’ll find that they’re often pretty specific. You’re going to run into these on a daily basis, and they’re a part of the compiler doing its job and helping you, so figuring out how to use them is a critical skill in programming.

Lets break one down to make that a little more approachable. For example:
6673459--764233--upload_2020-12-31_10-11-43.png

The first bit, “[11:17:58]” is a time stamp. That says when the error was run into. If you’re in the habit of clearing your console window then you can safely skip it, as all errors printed should be current anyway.

Next we have “Assets/Controll.cs(9,6)”. This bit is a location, and it’s super useful. It has two parts:

  • Assets/Controll.cs”: The path to the file the error was found in.
  • (9,6)” The line and column where the error was found.
    You only have one code file, so the first bit is a little redundant. But the second bit is super useful as it’s telling you where to start looking. In this case, line 9 character 6. Note that this is telling you where it realised things don’t make sense, which isn’t the same as where you need to apply the fix. (If the compiler could figure that out it probably wouldn’t be an error.)

Next we have an error description, which also comes in two parts:

  • errror CS1513”. This is an error code. Mostly useful if you need to look it up in the compiler documentation, which thankfully isn’t required often, thanks to…
  • } expected”. This is a description of the error, which tells you why the compiler got confused.

Now that you know the “where” and the “why” behind the compiler’s confusion you can combine it with your coding knowledge to figure out a fix. At first this is daunting and slow, but with practice you’ll get pretty fast.

In this particular case, it looks like there are two issues:

  • You’re declaring your variables inside your functions, where it looks like they should be outside.
  • You’re mixing up how things are done in in normal functions compared to how they’re done in properties, and have some extra braces and keywords in your code which don’t belong. You don’t need the “set { }” part around variable assignments.

To understand how to fix these errors yourself you’ll need to learn about variables and scoping.

P.S: You’ve posted this in the General Discussion section of the forum, which is specifically not a support area. This should go in the Scripting section. Details are hugely important in programming, so get in the habit of paying close attention to things.

4 Likes

Also, here’s a tip for fixing compile errors: always focus on the first one!

As I described above, the error is actually where the compiler got confused. From there it’ll try to make sense of the rest of the code, but things are already out of whack. What this means is that when you solve the first error it will sometimes solve some of the others as well, just by giving the compiler more context.

3 Likes

Thank you @angrypenguin for the help! it really helped!

1 Like

Also thank you @kdgalla ! I searched up how to set variables on google and got that answer somewhere. I guess I can’t trust that site anymore!

Just to be clear, “set” is a real keyword in C# but it’s not for asigning a value to a variable- it’s for something called a property, which is sort of like a variable but not exactly the same thing.

I see.

A property is actually a function or two. It’s actually one of my least favorite parts of C# because of pretty much this part of how they muddy things. We use them like variables, but they’re actually functions, and they use their own special bespoke syntax.