Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How to resolve "compiler bugs"?

Discussion in 'Scripting' started by LinuxDevJ, Jan 24, 2021.

  1. LinuxDevJ

    LinuxDevJ

    Joined:
    Sep 26, 2020
    Posts:
    23
    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?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    THAT will never happen. :)

    How to understand errors in general:

    https://forum.unity.com/threads/ass...3-syntax-error-expected.1039702/#post-6730855

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

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    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.
     
    Last edited: Jan 24, 2021
    angrypenguin and Bunny83 like this.
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Indeed, your compiler errors will show up in the Unity console window.
     
    angrypenguin and Kurt-Dekker like this.
  4. LinuxDevJ

    LinuxDevJ

    Joined:
    Sep 26, 2020
    Posts:
    23
    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<GameObject>' does not contain a definition for 'add' and no accessible extension method 'add' accepting a first argument of type 'List<GameObject>' 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.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    When you use an API, any API, always start with the documentation, first for List<T>.

    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/do...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.
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    LinuxDevJ and Kurt-Dekker like this.
  7. LinuxDevJ

    LinuxDevJ

    Joined:
    Sep 26, 2020
    Posts:
    23
    Oh boy, ... thanks! :rolleyes:

    Why did Microsoft do this? :mad: 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. o_O
     
  8. LinuxDevJ

    LinuxDevJ

    Joined:
    Sep 26, 2020
    Posts:
    23
    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.
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    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. :)

    This may help you with intellisense problems:

    https://forum.unity.com/threads/intellisense-not-working-with-visual-studio-fix.836599/

    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://forum.unity.com/threads/no-suggestions-in-vscode.955197/#post-6227874
     
    NOT_A_ROBOT1101 and PraetorBlue like this.
  10. LinuxDevJ

    LinuxDevJ

    Joined:
    Sep 26, 2020
    Posts:
    23
    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.
     
  11. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    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.
     
  12. NOT_A_ROBOT1101

    NOT_A_ROBOT1101

    Joined:
    Jun 18, 2020
    Posts:
    39
    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.
     
    Kurt-Dekker likes this.
  13. LinuxDevJ

    LinuxDevJ

    Joined:
    Sep 26, 2020
    Posts:
    23
    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?
     
  14. NOT_A_ROBOT1101

    NOT_A_ROBOT1101

    Joined:
    Jun 18, 2020
    Posts:
    39
    You need the VS Code Editor but I don't know about VS Editor.
    I have both and it's working just fine.
     
  15. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    They are two completely separate editors, and you can use either one. I personally use Visual Studio.
     
  16. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,509
    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.
     
    NOT_A_ROBOT1101 and Kurt-Dekker like this.