Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug [SOLVED] VS Code does not recognize Unity

Discussion in 'Editor & General Support' started by migellen, Aug 7, 2022.

  1. migellen

    migellen

    Joined:
    Sep 19, 2020
    Posts:
    8
    VS Code does not recognize anything from unity and I see error messages like "The type or namespace name 'MonoBehavior' could not be found (are you missing a using directive or an assembly reference?)" in Unity.

    I installed the latest version of Unity through Unity hub. I installed VS Code following the "Unity Development with VS Code" page (https://code.visualstudio.com/docs/other/unity).

    I have the .NET SDK installed (6.0). I have the .NET framework and targeting pack installed (4.7.1).

    I have tried deleting my project and making a new one. I have tried restarting. I have tried regenerating project files. I have only creating new files using "Open C# Project" in Unity.

    I am not seeing any output whatsoever in VS Code. VS Code briefly recognized Unity packages when I first installed the .NET framework but once I reopened it it no longer works.

    I have found multiple posts on the internet and have found no solutions:

    https://forum.unity.com/threads/not...sense-on-vscode-with-unity-2019-3-0a8.787643/
    https://forum.unity.com/threads/vscode-does-not-recognize-unity-namespaces-unity-package.1172075/
    https://stackoverflow.com/questions...e-found-by-omnisharp-even-though-unity-builds
    https://forum.unity.com/threads/intellisense-not-working-for-visual-studio-code.812040/
    https://stackoverflow.com/questions...-is-not-finding-referenced-projects-in-vscode

    Does anyone know how I can fix this? Why doesn't unity have straightforward method of installing with Visual Studio?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,519
    This is for Visual Studio but there might be something for VS Code:

    This may help you with intellisense and possibly other Visual Studio integration problems:

    Sometimes the fix is as simple as doing Assets -> Open C# Project from Unity. Other times it requires more.

    Other times it requires you also nuke the userprefs and .vsconfig and other crufty low-value high-hassle files that Visual Studio tends to slowly damage over time, then try the above trick.

    Barring all that, move on to other ideas:

    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
     
  3. greg-harding

    greg-harding

    Joined:
    Apr 11, 2013
    Posts:
    523
    On Mac, to get Intellisense working in VS Code Make sure you have installed Mono on your system and in the VS Code settings for Omnisharp set 'Use Global Mono' to 'always'. I'm currently using the slightly older C# extension for VS Code / Omnisharp v1.24.4 because the latest v1.25 seemed to break intellisense for Unity again (might be the 'useModernNet' setting the extension mentions, haven't checked yet). This might be relevant for Windows too.

    Also, when you open a project check the VS Code output window set to 'Omnisharp Log' and you should see if it's starting ok or not.
     
  4. migellen

    migellen

    Joined:
    Sep 19, 2020
    Posts:
    8
    @
    Thanks for the suggestions! I'll try as soon as I get back to my PC. If that doesn't work I'll uninstall VS Code, Unity, and the .NET packages and start from scratch.


    I'm on Windows, I'll see if the Omnisharp settings help. Thanks for the VS Code output suggestions, I'm not sure why I was not seeing any output there.



    I'll update this post once I'm tried these suggestions. Thanks for the help!
     
  5. migellen

    migellen

    Joined:
    Sep 19, 2020
    Posts:
    8
    Ok, I made some progress

    I tried `Assets -> Open C# Project from Unity`, that didn't help.

    I then looked at the actual output (thanks a lot greg-harding for pointing out that I needed to look at the Omnisharp output). I googled the errors I saw there and it aligned with Kurt-Dekker's second link (https://forum.unity.com/threads/no-suggestions-in-vscode.955197/#post-6227874) and greg-harding's suggestion - mono is needed. I went to https://www.mono-project.com/download/stable/, clicked on `Download Mono 64-bit (no GTK#)`, and installed Mono. I reopened my C# file (using open C# project) and now `using UnityEngine` and `
    MonoBehaviour` is recognized, but "Tile" still isn't. I tried using `GameObject` and that is recognized though.

    tile_error.png

    I didn't run `export FrameworkPathOverride=/Library/Frameworks/Mono.framework/Versions/Current` in Kurt-Dekker's second link, I wasn't sure where I was supposed to run that command. I couldn't run it from the VS code terminal, Windows command prompt, or the Mono command prompt. Where can I run this export command?

    I also did not see any `Use Global Mono` setting, but I did see a `Omnisharp: Mono Path` setting that seemed to be relevant. Does that need to be specified, or is this Tile error not relevant to Mono? I am still seeing the `The type or namespace name 'Tile' could not be found (are you missing a using directive or an assembly reference?)` error in Unity.
     
    Last edited: Aug 8, 2022
    abhinandansj likes this.
  6. greg-harding

    greg-harding

    Joined:
    Apr 11, 2013
    Posts:
    523
    When you have the VS Code settings open search for 'Mono' and the 'Use Global Mono' should appear near the top. On Mac there's a bug opening more than one project at the same time if you don't set the 'Mono Path' (even if it's using the same default path) but I don't think this is an issue on Windows.

    Check if you're using the C# Omnisharp extension v1.25 and if so then read the info/details for how to set 'useModernNet' to false to support Unity stuff. I'm not using this version but that's what the latest version says to use.
     
  7. migellen

    migellen

    Joined:
    Sep 19, 2020
    Posts:
    8
    I checked the C# extension I had installed and it says the omnisharp version is version 1.39.0. I have already set `Use Modern Net` false as suggested by the VS Code Unity install guide. These are the options I see when I search for Mono:
    mono_search_settings.png

    It looks like `MonoBehavior` is working now, but it's flakey. I have to reload VS Code or regenerate project files often.
    I also can't get Unity or VS Code to recognize `Tile`. I'm still seeing `The type or namespace name 'Tile' could not be found` errors. I'm not sure if this is a VS issue since I see the error on Unity as well - is something missing in Unity to support Tile? I see that "2D Tilemap Editor" (1.0.0) is installed on the Package Manager.

    Do you recommend continuing trying to make VS Code work or should I just try regular VS (2019)?
     
  8. migellen

    migellen

    Joined:
    Sep 19, 2020
    Posts:
    8
    Ok I figured it out my problem. I was following a tutorial off youtube (
    ) and I did not realize that "Tile" was not a built in type - I needed to create another script named "Tile.cs" in order to reference it in my GridManager class.

    I'm still not sure where to run that `export` command, but I think I should be good. I just decided to continue with regular VS instead of VS Code.

    Thank you very much for the help!
     
  9. abhinandansj

    abhinandansj

    Joined:
    Feb 12, 2014
    Posts:
    2
    @migellen
    Did you figure out finally?
    I faced exact same issue as you posted in earliest ;
    Your subsequent posts helped me a lot to get started
    I understood only missing thing was Intelligence and VS code acting like regular edit with familiarity for Unity
    For me It worked after I Installed VS Code editor in Unity->Window->Package manager
    PS: The package manager started showing the list only when i changed Packages to "Unity Registry" at the top