Search Unity

Can't get VSCode to work properly with Unity

Discussion in 'Linux' started by Kurolox, Jun 28, 2018.

  1. hollowsaibot

    hollowsaibot

    Joined:
    Feb 25, 2013
    Posts:
    2
    My version of the arguments are:

    $(ProjectPath) -g $(File):$(Line)

    (*without quotes. I didn't check if was already answered below, but it can help anyway)
     
  2. YordleGunner

    YordleGunner

    Joined:
    Apr 14, 2020
    Posts:
    1
    i got it working by:

    0 - dotnet sdk -> https://dotnet.microsoft.com/download
    1 - uninstall vs code from snap and install the .deb
    2 - going to edid -> preferences -> external tools, select the vs code executable
    3 - external args: "$(ProjectPath)" -g "$(File)":$(Line):$(Column)
    4 - on generate .csproj files for -> enable embedded packages, local packages and built-in packages. then click regenerate project files
    5 - installing the c# extension version 1.22.1 and debugger for unity 2.7.5

    that was it, intellisence and debug working
     
  3. losingisfun

    losingisfun

    Joined:
    May 26, 2016
    Posts:
    36
    Wow, this got overlooked. Exactly the issue for me. Bumping this because it solved the issue on several projects I had this problem with.
     
  4. miniwolf_unity

    miniwolf_unity

    Unity Technologies

    Joined:
    Apr 10, 2018
    Posts:
    138
    This issue should be fixed in the newest version of the Unity package for VSCode. 1.2.1. If this is not the case, please submit a bug so we can investigate this.
     
  5. axlSoft

    axlSoft

    Joined:
    Dec 26, 2017
    Posts:
    2
    OMG, you saved my life.

    thanks.... ;)
     
  6. MarcusValerius

    MarcusValerius

    Joined:
    Aug 1, 2016
    Posts:
    24
    VSCode stopped working for me last week. I was on Ubuntu 19.10, so I figured it was due to the official Mono support being dropped, and so I moved on to Ubuntu 20.04.1 LTS.
    However, it turns out that I can't get VSCode to work on this version either. I tried with both Unity2019.4.9f1 and Unity2020.1.3f1 as well as the latest version of VSCode and version 1.23.0 and v1.23.1 of the C# extension.
    I tried installing VSCode from the official .deb file (which Unity somehow cannot find at /usr/bin/code nor at /usr/share/code), I tried the Snapstore version and the Flatpak version, too.

    I always end up getting the exact same OmniSharp error log, and I don't know what to make of it:

    Is anyone getting Unity to work on Ubuntu20.04.1 at all?
     
    Last edited: Aug 31, 2020
  7. JooleanLogic

    JooleanLogic

    Joined:
    Mar 1, 2018
    Posts:
    447
    I'm on mint-ubuntu 20.04 and it's working fine with intellisense.

    Does libmono-native.so exist at the location specified in exception?
    Whenever vscode hasn't worked after an upgrade (every time), installing the dependencies has always resolved the issue. Usually installing mono-devel solves most problems for me. If that still don't work, go through the list of dependencies in the Release Notes thread and make sure they're all installed.

    One new change in latest vscode is that I also had to set 'Omnisharp: Use Global Mono' setting to 'always' so that it used the system installed mono instead of the packaged one (at least I think that's what it does). Otherwise I get the 4.7.1 framework error.
     
  8. MadWatch

    MadWatch

    Joined:
    May 26, 2016
    Posts:
    112
    I also got my VSCode broken recently. That happened just after Omnisharp updated itself. I fixed the problem by forcing the C# extension to an earlier version.
     
  9. JooleanLogic

    JooleanLogic

    Joined:
    Mar 1, 2018
    Posts:
    447
    Have you tried the 'Use Global Mono' setting I mentioned above. Mine broke on the latest update too but changing that setting fixed it.
     
  10. MarcusValerius

    MarcusValerius

    Joined:
    Aug 1, 2016
    Posts:
    24
    So after some investigation I noticed the following:
    • When trying to open VSCode installed from the official .deb file, the install directory (/usr/share/code/) doesn't show up in the "Browse for application" window in Unity's Preferences as if it was hidden. Therefore I cannot navigate to the executable. With the "external script editor" option set to its default setting ("vscode-editor"), Unity is unable to detect this version of VSCode and demands an installation via the Software Center.
    • The only version of VSCode that Unity recognizes automatically is the Flatpak version if installed from the Ubuntu software center (gnome-software). The SnapStore version, if installed, can be used as well after manually navigating to the executable located at /snap/code/current/usr/share/code. However, these two versions are both unable to recognize the dotnet that is installed on the system at /usr/bin/dotnet. Typing "dotnet --version" in VSCode's terminal in either of the versions results in this error: "dotnet: command not found". "which dotnet" yields no findings. The .deb version of VSCode, if launched from outside Unity, detects the installed dotnet just fine.
    In Snapstore & Flatpack VSCode:
    • Using the most recent version of the C# extension and setting 'Omnisharp: Use Global Mono' to 'always' results in the following error:
      The latest mono was installed on the system using these instructions: https://www.mono-project.com/download/stable/#download-lin-ubuntu
    Yes, it's there. VSCode (flatpak & snapstore) can't seem to access the folder.

    UPDATE:
    Success! :)
    After realizing that my issues were somehow related to access rights of the Hub and/or Editor, I tried a few more combinations and ended up finding something that actually works for me on Ubuntu 20.04.1:
    • official UnityHub.AppImage (chmod +x)
    • SnapStore VSCode with Unity Debugger extension as well as the latest C# extension with 'Omnisharp: Use Global Mono' set to 'always' (The latter is a must!)
    • in Unity's preferences manually browsing to the VSCode executable at /snap/bin/code
    • args: "$(ProjectPath)" -g "$(File)":$(Line):$(Column)
    • no additonal csproj files generated
    Unlike the Flatpak UnityHub, the appimage possesses all the rights to access root folders and find the executable.
    The slightest deviation from this setup would immediately result in all kinds of errors like not showing the correct VSCode executables, and VSCode telling me that dotnet and/or mono couldn't be found. ("Cannot start OmniSharp because Mono version >=6.4.0 is required." etc.)
     
    Last edited: Oct 20, 2020
  11. zessirb

    zessirb

    Joined:
    Dec 1, 2018
    Posts:
    1
    Nice, finally fixed it for me. Can't believe I missed that setting for so long (although I have a "Regenerate project files" button below "Generate .csproj files for:" instead, with some checkboxes to check)
    Also, I sometimes had the Omnisharp error (looking for a dotnet 4.x.x version it did not have), and changing the vscode setting "Use global mono" to "always" seems to fix it.
    Thanks!

    For those still having that kind of issue, it might be useful to try a basic C# program with mono: https://www.mono-project.com/docs/getting-started/mono-basics/ (you don't need to install dotnet, but you need mono)
    If the hello world works, then your issue is probably on Unity/vscode side
     
    Last edited: Sep 8, 2020
    TCROC likes this.
  12. TCROC

    TCROC

    Joined:
    Aug 15, 2015
    Posts:
    230
    Nice! The "Regenerate Project files" fixed it for me. I had to regenerate them after importing a new UPM package.
     
  13. bolska

    bolska

    Joined:
    Jul 19, 2018
    Posts:
    8
    I solved this problem removing spaces in my project name. Renamed "Project Name" to "ProjectName". Then I opened the project on unity, then opened vscode through unity and the problem was fixed.
     
  14. Hassan-Kanso

    Hassan-Kanso

    Joined:
    Jun 10, 2014
    Posts:
    11
    if you have installed VSCode through Ubuntu Software then go to Edit -> Preferences, then select the External tools tab in Unity
    then browse external editor to
    /snap/bin/code

    don't mistake it with the snap folder in your home directory! (as I did)
     
  15. billclock

    billclock

    Joined:
    Nov 30, 2017
    Posts:
    4
  16. billclock

    billclock

    Joined:
    Nov 30, 2017
    Posts:
    4
  17. JeriUnity

    JeriUnity

    Joined:
    Dec 4, 2015
    Posts:
    3
    When following a new book using VSC with Unity I couldn't get VSC (updated) to debug because of the launch.json file, even though there was one there. I put a copy of what someone on a forum had done, but that didn't work either. Someone mentioned VS Community so I downloaded that (which got updated). Everything worked fine after that. My VS community is version 16.9.0 Preview 2.0 and my Unity is 2020.2.0b14.3668
     
  18. JeriUnity

    JeriUnity

    Joined:
    Dec 4, 2015
    Posts:
    3
    I just could not get VSC to debug. Someone mentioned VS Community helped so I downloaded that and now everything works well, including auto updating Unity when leaving VSC. This is all using the latest versions of Unity and VSC as of the date of this message.
     
  19. Achie1

    Achie1

    Joined:
    Jan 14, 2018
    Posts:
    26
    I use Ubuntu 2020.10. I have the latest VSCODE (1.53) and .NET core SDK 5.0.102. Everything works fine. This is what I did.
    1. In VSCODE extension panel, go to C# for Visual Studio code extension.
    2. Click on the gear icon and Click on "Install another Version".
    3. Chose version 1.22.1.
    This version works fine for me. I don't have Intellisense problem. But when I chose other version, I lose Intellisense.
     
    MrJohnWeez likes this.
  20. RAscended

    RAscended

    Joined:
    Feb 5, 2021
    Posts:
    9
    Maybe i am late. Maybe not. If you can, please, explain what you meant in this part "Ignore all advice given on other threads where you create an alias or a symbolic link to add custom arguments. Instead of that, in the External Script Editor field, select
    /usr/bin/code
    "
     
  21. srvkataria

    srvkataria

    Joined:
    Jan 11, 2021
    Posts:
    1
    This worked for me ---

    The idea is.. even if you close unity application, intellisense should still work for unity projects opened in VS code.

    You will need to set "omnisharp.useGlobalMono" to "always".

    For those wondering where the setting omnisharp.useGlobalMono should be set: go to VSCode, click on Preferences > Settings, and then use the search bar to search for omnisharp.useGlobalMono. Then you should see the dropdown and set it to always.

    My VS code version: 1.53.2
    Unity version: 2019.4.17
     
    Egad_McDad, GeneBox and Radzievskyi like this.
  22. thenoctus1984

    thenoctus1984

    Joined:
    Oct 4, 2021
    Posts:
    1
    I was having issues with this as well. I ended up with .net dev 4.71 as recommended above but what finally fixed it was going into unity -> preferences -> general -> Regenerate Project Files.

    not sure if .net 4.71 allowed the regenerating to work or not, but at this point I don't care ;)
     
  23. radoslavchobanov

    radoslavchobanov

    Joined:
    Dec 23, 2020
    Posts:
    1
    Thank you !!!
     
  24. nrader95

    nrader95

    Joined:
    Aug 19, 2019
    Posts:
    24
    I'd say the proper way of using vscode with unity is to not use unity at all.

    What i mean by that is that you can set up dedicated git repo with all your code in it, place unity engine DLLs deps in some folder, and than to reference them in csproj (mark them as non-private to make sure dotnet wont copy them into output folder). like this:

    Code (XML):
    1.     <ItemGroup>
    2.         <Reference Include="UnityEngine">
    3.             <HintPath>..\..\dependencies\UnityEngine\UnityEngine.CoreModule.dll</HintPath>
    4.             <Private>False</Private>
    5.         </Reference>
    6.     </ItemGroup>
    And post-build settings in csproj to auto-copy new dll into unity proj:
    Code (XML):
    1.     <Target Name="PostBuild" AfterTargets="PostBuildEvent">
    2.         <Exec Condition=" '$(OS)' != 'Windows_NT' " Command="cp $(OutDir)* ../../your-unity-proj/Assets/Your-Folder-For-DLLs/" />
    3.     </Target>
    4.  
    Now you don't have to worry about having vscode intergation package, having right IDE prefs in unity or other things. As a bonus, now you can work on any potato PC since you dont need to have unity running at all times.

    There is one exception to this and one thing to note.
    First is debugging. Although the unity debugger extension can work with such scenario, it wont be able to auto-generate launch.json for you properly, and that's important, because that's where the location to the EditorInstance.json file where your current unity instance PID is specified for debugger to attach properly. (you can use "Unity Attack Debugger" from menu without configuring anything, tho)
    Btw, with Unity 2021+, debugger extension, although working, for some reason wont detect unity, unless you enable debugging beforehand.

    So you will have to write something like this:

    Code (Json):
    1. {
    2.     "version": "0.2.0",
    3.     "configurations": [
    4.         {
    5.             "name": "Unity Editor",
    6.             "type": "unity", // needs unity debugger extension
    7.             "path": "${workspaceFolder}/relative-path-to-unity-project/Library/EditorInstance.json",
    8.             "request": "launch"
    9.         }
    10.     ]
    11. }
    And then commit to to your repo, since anyone with vscode will need it. Keep in mind that for this to work, you will have to always place unity project at the same place(relatively to your code repo)

    Second thing to note is that since we are gonna build DLL beforehand and wont let unity interact with .cs files, the compiler directives like #if UNITY_EDITOR, although kinda usable, but will bring more hassle than its worth. (And placing editor scripts in folders named "Editor" wont work anymore, too, ofc). So i would recommend to set up separate csproj with all editor stuff and mark this DLL in unity to be used only in editor, lest in will fail your builds.

    P.S. That's kinda troublesome setup, i know, but if you do that, such thing as "Official unity support for my IDE" wont bother you anymore, you can even use IDEs that are not supported at all, and things will still work like charm. (Even debugging, you can debug unity without unity debugger extention too, Unity Editor actually use usual Mono debugging functions for that, with the debugging local port being 56XXX, where XXX is last three digits of unity instance PID.)
     
  25. LEMMIIX

    LEMMIIX

    Joined:
    Jun 6, 2022
    Posts:
    9
    June 2022 this still works perfectly. Huge thanks!
     
  26. mikey555

    mikey555

    Joined:
    Oct 9, 2014
    Posts:
    3
    I too have had issues getting Intellisense to work in Visual Studio Code. The reason is that I didn't have External Script Editor set correctly, and therefore .csproj files weren't being regenerated. .csproj will be updated when you make new scripts, and is necessary in for using Intellisense.

    The issue I had is that Visual Studio Code didn't show up on the External Script Editor menu. When I clicked Browse and selected the application in Applications, it appeared as
    Code (internal)
    and had none of the csproj generation options. I had to Show Contents on my VSCode application to find the location of the binary:
    Visual Studio Code 2/Contents/Resources/app/bin/code

    When I entered this in the External Script Editor menu, everything worked.

    As a side note, the popular solution here says to use
    /usr/bin/code
    . This assumes that you have a symlink for the VSCode binary already created. You may have created this symlink by messing around with other responses in this thread, maybe it's there by default, or maybe it's not there at all. If it's not there, go into the VSCode app and find the path to its binary like I explain above.
     
  27. unlucky00gmail

    unlucky00gmail

    Joined:
    Sep 25, 2020
    Posts:
    6
    Just to confirm that usr/bin/code is for VS not VSCode right? Because I am having trouble finding it.
     
  28. nrader95

    nrader95

    Joined:
    Aug 19, 2019
    Posts:
    24
    Ofc not, linux version of regular VS simply does not exist, so there is only VSCode.
     
  29. Futurristic

    Futurristic

    Joined:
    Jun 21, 2016
    Posts:
    47
    This solves my problem, Thank you very much...:)
     
  30. Zephni

    Zephni

    Joined:
    Apr 23, 2015
    Posts:
    100
    If anyone is still having an issue with this (I have for the last 6 months or so, so I keep having to use Visual Studio instead), but this is the thing that fixed it for me.

    Following the prerequisites here: https://code.visualstudio.com/docs/other/unity

    It tells you in step 5:

    upload_2022-8-4_10-32-8.png

    So I did that:

    upload_2022-8-4_10-30-14.png

    Then restarted VSCode, and now it works! Feels so freeing to finally find a solution :) If anyone else is having this problem I suggest trying this, and taking a look at the link I posted above if they are having any further issues.
     
    MiltonPulgarImova likes this.
  31. MiltonPulgarImova

    MiltonPulgarImova

    Joined:
    Nov 2, 2020
    Posts:
    6
    TX!. I did the steps: 2, 3 and 4. Then, in Unity Assets->Open C# Project

    Note: Before that, I followed the official instructions in this page: Visual Studio Code and Unity
     
  32. ColMaxon

    ColMaxon

    Joined:
    Yesterday
    Posts:
    1
    Thank you friend. Love you