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?
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.
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.
Why did Microsoft do this? 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.
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.
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.
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.