How to resolve "compiler bugs"?

I set up VS Code to edit my C# scripts. As long as I don’t make any mistakes, my littel game works as expected. Now I have a bug in a script. Unity just flashes this messages when I try to “play”:

All compiler errors have to be fixed before you can enter playmode!

I also set up VS Code “debug mode”, set my breakpoints and start VS Code in “debug mode” … but it does not halt at the breakpoints and also does not show errors:

UnityDebug: Initializing
UnityDebug: Searching for Unity process 'Unity Editor'
UnityDebug: Attached to Unity process 'Unity Editor (Unity)' (45923)
Resolved pending breakpoint at '/home/Daten/Projekte/Unity/Games/Demo4-2D-Snake/Assets/Scripts/Snake.cs:89,1' to void Snake.OnTriggerEnter2D (UnityEngine.Collider2D coll) [0x00024].
Resolved pending breakpoint at '/home/Daten/Projekte/Unity/Games/Demo4-2D-Snake/Assets/Scripts/SpawnFood.cs:51,1' to void SpawnFood.Spawn () [0x0006d]. 
...

and only pops up an error dialog with the same message:

C# Debugger Attached
All compiler errorsd must be fixed before switching to debug mode.

How can I debug my code if I FIRST have to fix erros??? I wonder if Unity has an own compiler output error view to help pointing to the script with the error and the location?

THAT will never happen. :slight_smile:

How to understand errors in general:

https://discussions.unity.com/t/824586/8

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: https://discussions.unity.com/t/481379

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

Until you fix the syntax errors, there is literally no executable code to run. It has not been created. Scripts are just a bunch of text files until they are compiled into executable code. That can’t happen until all compiler errors are gone. That’s how compiled programs work. Interpreted programs can be run up until their error, in some cases. Not so much with C#.

ALSO: fixing compiler errors is NOT debugging. Compiler errors are NOT bugs. Debugging is running a functioning program that has a runtime error and figuring out that error. That’s debugging.

2 Likes

Indeed, your compiler errors will show up in the Unity console window.

2 Likes

Yes, I noticed the small line in Unity at the very bottom, when I change to Unity … but what is a good way to do this? I was told to use VS Code for scripting, so I used this external editor. But this editor does not show any syntax errors for my C# scripts. I would need a full fledged “Unity C# IDE” which I am not aware of.

In my case I try to introduce a static List:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/* This script is used as a singleton global instance.
*/
public static class Globals {
/* All game objects to destroy on restart.
*/
public static List<GameObject> objects2Destroy = new List<GameObject>();

}//Globals

and try to add the instantiated GameObject in another class via

Globals.objects2Destroy.add(g);

but this does not seem to compile:

“Assets/Scripts/SpawnFood.cs(51,33): error CS1061: ‘List’ does not contain a definition for ‘add’ and no accessible extension method ‘add’ accepting a first argument of type ‘List’ could be found (are you missing a using directive or an assembly reference?)”

As I know how to code in Java with a Java IDE like Eclipse, I am a rookie in C# and I miss such an idea where I get more error info and access the API via a shortcut. (I can’t find the API for List … but from other code I know that there is an “add()” method.

When you use an API, any API, always start with the documentation, first for List.

https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1?view=net-5.0

In this case, look at the method you are trying to use, “add” as you have used it:

https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.add?view=net-5.0#System_Collections_Generic_List_1_Add__0_

Note the capitalization. In programming everything must be spelled, capitalized and punctuated 100% correct, not even 99.999% is good enough.

In this case add is not Add so you have an error.

This is your problem. Your Java instincts are telling you that a method name should start with a lowercase letter. Unfortunately, in C# world, methods by convention start with a capital letter. In this case, List.Add.
https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.add?view=net-5.0

2 Likes

Oh boy, … thanks! :roll_eyes:

Why did Microsoft do this? :rage: Everywhere I read “C# is about 99% Java … so it’s very easy to code in C# as a Java Developer” … this does not seem to be true. :eyes:

So, what is a good way / tool to write / compile / debug the C# code within Unity? I am used to get instant feedback from my Java IDE when the syntax has errors.

Properly configured, Visual Studio will do all this for you: suggest completions, show red squiggly lines under errors, etc.

Microsoft’s term for it is Intellisense and I believe VSCode does it too.

The trick is the ‘properly configured’ part. :slight_smile:

This may help you with intellisense problems:

https://discussions.unity.com/t/778503

Also, try update the VSCode package inside of Unity: Window → Package Manager → Search for Visual Studio Code Editor → Press the Update button

Also, this: https://discussions.unity.com/t/805330/7

2 Likes

Please see my signature: I am running Linux - not Windows. SO I don’t have “Visual Studio”, but “only” VSCode.

I wonder how big games are developed with Unity if the code/script part is so poorly supported , i.e. without a full fledged built in IDE. Intellisende somehow works in VSCode but only for a part of the API. No red squiggly lines, etc. That makes it very hard to find bugs in scripts or debug them.

At the end of the day it’s just C#… maybe Mono can be a better editor for you on Linux? Or whatever the state of C# development is on Linux. I imagine there’s something and you can tell Unity use any editor you like in the preferences.

Then there is a problem with the config either Unity, VSCode, or both. Go to Edit > Preferences… > External Editor and then select Visual Studio Code as your code editor. Also, don’t forget to install the C# extension on VSCode and the VSCode package on Unity (which happens automatically for me).
To check the VSCode package on Unity:
Window > Package Manager
Click the package scope drop-down menu and select In Project. You should see Visual Studio Code in the list of packages.
If not, click the package scope drop-down menu again and select Unity Registry, then install the Visual Studio Code package.

1 Like

Everything was already there / installed (green check mark). I wonder why there are two installed:

A) “Visual Studio Code Editor” 1.2.3
B)“Visual Studio Editor” 2.0.5

Do I need both?

You need the VS Code Editor but I don’t know about VS Editor.
I have both and it’s working just fine.

They are two completely separate editors, and you can use either one. I personally use Visual Studio.

Do you have the “Console” window open in Unity? If you’re writing code then that should be visible whenever the Editor is open. That will give you a list of all compile errors in your scripts, and you can double-click an error or a line in its stack trace to go straight to the relevant script file.

It’s also useful for monitoring runtime behaviour and/or debugging errors using Debug.Log(…) and co.

As for how big games are developed, well… there is support for full code IDEs. Most “big games” are made using Windows or Mac machines where these tools generally work out-of-the-box with no special configuration from users. I double-click a log entry or a code file or a texture or whatever from Unity and it opens the correct editor with the right file ready for me to do my thing.

Unity doesn’t build in its own code IDE for the same reason that they haven’t re-built Photoshop and Maya and Audacity. It doesn’t make sense to re-invent those wheels when there are already well established programs to do all of that stuff which people and studios already have pipelines and workflows for.

2 Likes