Search Unity

How to change VSCode's TargetFrameworkVersion

Discussion in 'Code Editors & IDEs' started by zIyaGtVm, Dec 17, 2018.

  1. zIyaGtVm

    zIyaGtVm

    Joined:
    Dec 27, 2017
    Posts:
    131
    Hi, I just installed Unity2018.3 and reinstalled VisualStudio 2017 with frameworkVersion 4.7.1.

    When I create a script in unity and open it by VSCode, errors turn out:
    Then I found VSCode changed Assembly-CSharp.csproj automatically.
    Even though I tried to open new script in VS2017 first, VSCode will reset it from v4.7.1 to v4.5.:confused:

    Anyone knows how to avoid this?Thanks!

    Unity version 2018.3.0f2
    VScode versio 1.30.0
     
  2. StevenDelrue

    StevenDelrue

    Joined:
    Aug 21, 2014
    Posts:
    16
    You can manually set the framework to 4.7.1 in the project-file and the error will be gone.
    But the csproj-file gets regerenerated each time you reopen the project, so it's quite annoying :/ Would be nice if you manually set the Target Framework Version in Unity
     
  3. zIyaGtVm

    zIyaGtVm

    Joined:
    Dec 27, 2017
    Posts:
    131
    Yes I manually set the framework to 4.7.1 in the project-file by texteditor, and it gets regerenerated.
    Would you tell me where to set the Target Framework Version in Unity?Thanks!
    u.png
     
  4. tantx

    tantx

    Joined:
    Jul 5, 2017
    Posts:
    23
  5. zIyaGtVm

    zIyaGtVm

    Joined:
    Dec 27, 2017
    Posts:
    131
  6. tantx

    tantx

    Joined:
    Jul 5, 2017
    Posts:
    23
  7. zIyaGtVm

    zIyaGtVm

    Joined:
    Dec 27, 2017
    Posts:
    131
    anycolourulike likes this.
  8. StevenDelrue

    StevenDelrue

    Joined:
    Aug 21, 2014
    Posts:
    16
    I reinstalled visual studio community (studio, not code) with the .net frameworks and the error is gone now
     
  9. rainssong

    rainssong

    Joined:
    Nov 6, 2013
    Posts:
    6
  10. Halcomb

    Halcomb

    Joined:
    Jan 23, 2020
    Posts:
    3
    You can edit a Visual Studio C++ project file to target different versions of the C++ platform toolset, the Windows SDK and the .NET Framework (C++/CLI projects only). By default, the project system uses the .NET Framework version and the auto clicker toolset version that correspond to the version of Visual Studio that you use to create the project. You can modify all these values in the .vcxproj file so that you can use the same code base for every compilation target.
     
    Last edited: Jan 31, 2020
  11. SirIntruder

    SirIntruder

    Joined:
    Aug 16, 2013
    Posts:
    49
    Having plenty of experience with this, one thing that seems to work most reliably is to go:

    VS Installer -> dropdown next to your latest VS installation -> Modify -> Individual components tab -> install all missing targeting packs (see attached screenshot)

    Unlike manual installation of sdks, this just seems to work.

    If you see 4.5.1 installed and it still doesn't work, it's likely there are multiple VS installations on your pc, and system is looking up another installation of Visual Studio. Get rid of all old Visual Studio installations and make sure the ones you use have needed targeting packs
     

    Attached Files:

    • vs.png
      vs.png
      File size:
      72.4 KB
      Views:
      1,039
    Dio330go and Deleted User like this.
  12. Fattie

    Fattie

    Joined:
    Jul 5, 2012
    Posts:
    476
    INSANE SOLUTION YOU WON'T BELIEVE IF ON MAC

    1. Download Rider (no, really)

    2. Install the Rider package manager inside Unity

    3. Probably restart every electronic device in your house

    4. Launch Unity, set the external script editor as Rider - launch

    5. Use Rider a few times, save, change files etc.

    6. Quit RIder and Unity

    7. Launch Unity - set the external script editor back to VSCode

    8. In the Project panel, double-click on a c# file, VSCode will open

    THE PROBLEM IS FIXED, VSCODE NOW WORKS

    WTF? Hope it saves someone some hours
     
    anycolourulike likes this.
  13. tbngrd

    tbngrd

    Joined:
    Jul 25, 2018
    Posts:
    12
    WTF indeed - from all the solutions online this is the only one that worked perfectly for me. Thank you so much!

    Now what do we do if it happens again and the Rider trial version is over? Lol.
     
  14. Fattie

    Fattie

    Joined:
    Jul 5, 2012
    Posts:
    476
    @tbngrd it's a good question !

    I THINK the same applies if you actually use visual studio ... so I will try having both VS, and VSCode on my machine. I will "change to" VS when the problem arises!

    By the way on my machines, every single time I happen to add a new C# file, the VSCode system breaks, and I have to do the Rider Trick. What a fiasco :/

    the whole thing is so stupid .. wake up, Unity ..
     
    anycolourulike likes this.
  15. L_Bens_Up

    L_Bens_Up

    Joined:
    Sep 14, 2020
    Posts:
    1
    I had this problem before, i had to change the Target Framework Version in the Assembly-CSharp.csproj every time i opened a new script in VS Code, i had the 4.8 version but it kept getting set back to 4.7.1 form some reason ....

    i managed to fix this by going to the package manager and removing the visual studio package from my project, after that every time i opened a script, i found the target framework version to be 4.8 ....
     
    anycolourulike likes this.
  16. nackerboss06

    nackerboss06

    Joined:
    Jun 4, 2021
    Posts:
    13
    not for me ;(
     
  17. Deleted User

    Deleted User

    Guest

    I manually installed 4.8 but the Target Framework Version in the Assembly-CSharp.csproj always reseted to 4.7.1 when I added a new script. And I couldn't manually install 4.7.1 because it said I already have a newer version installed and I didn't want to uninstall 4.8.

    With your solution I easily could install 4.7.1 even without installing Visual Studio! Thanks!
     
  18. aecanales

    aecanales

    Joined:
    May 27, 2020
    Posts:
    2
    Since this is one of the top results on Google, in case anyone else comes from the future with the same problem, the second answer in rainsong's link worked for me: https://gamedev.stackexchange.com/a/197737
    Looking at the source for the VSCode plugin, it seems as the target framework version is a hard-coded constant, so one way to fix it is by moving the package to your local package folder instead of the library cache and editing the source code to your targeted framework as explained in the stackexchange link.
     
    anycolourulike likes this.
  19. leandiez

    leandiez

    Joined:
    Jan 17, 2019
    Posts:
    4
    Hi to all.
    I've opened an Issue on the GitHub of the Package to solve the hard coded FrameworkVersion.
    IDK if will have an answer or a solution, but if someone have time could try to commit the change through a Pull Request or suggest how to solve this.

    Link to Issue: https://github.com/needle-mirror/com.unity.ide.visualstudio/issues/1
     
  20. swedish_boy

    swedish_boy

    Joined:
    Nov 10, 2017
    Posts:
    3
    Another potential fix is just to regenerate the project files from the External Tools under Unity Preferences. This worked for me with Unity 2020.3 and Visual Studio 2019 (8.10.19) on macOS.
     
  21. swedishfisk

    swedishfisk

    Joined:
    Oct 14, 2016
    Posts:
    57
    The GitHub issue was closed:

    Does anyone know if there is an open ticket on the Unity side for this? I'm afraid it slipped through the cracks and this is very big issue for anyone who uses VSCode and happens to have a dependency that is compiled against 4.8.
     
  22. swedishfisk

    swedishfisk

    Joined:
    Oct 14, 2016
    Posts:
    57
    Hey guys I have to bump this now, I've found a workaround to add a propertygroup to the *.csproj file:

    Code (CSharp):
    1. <ResolveAssemblyReferenceIgnoreTargetFrameworkAttributeVersionMismatch>true</ResolveAssemblyReferenceIgnoreTargetFrameworkAttributeVersionMismatch>
    Unfortunately this gets overwritten, is there really no way of working with VSCode in Unity when one of your dependencies are compiled against a .net version later than
    v4.7.1?

    The issue seems to be that the *.csproj file always gets autogenerated with this property:

    Code (CSharp):
    1. <TargetFrameworkVersion>v4.7.1</TargetFrameworkVersion>
     
  23. CTN-Originals

    CTN-Originals

    Joined:
    May 30, 2020
    Posts:
    6
  24. MrSalad7

    MrSalad7

    Joined:
    Jan 23, 2022
    Posts:
    1
    If anyone comes to this problem, here's my solution:
    1. Close your VSCode Projects
    2. Go to */YOURUNITYPROJECT/"Library/PackageCache/com.unity.ide.vscode@1.2.4/Editor/ProjectGeneration/ProjectGeneration.cs" There are sometimes multiple ProjectGeneration.cs files so go to that path exactly
    3. open ProjectGenerations.cs in VSCode (NOT ProjectGenerationFlags.cs)
    4. go to
      const string k_TargetFrameworkVersion = "v4.x.x";
      (line 146 on my pc)
    5. change the string to your framework version (...= "v4.8" for example)
    6. Save and close
    7. on Unity go to Assets -> Open C# Project
    8. as you'll see on the .csproj files the Version is now your desired one