Search Unity

Resolved VSCode does not recognize Unity Namespaces [Unity Package]

Discussion in 'Code Editors & IDEs' started by MichaelTGD, Sep 20, 2021.

  1. MichaelTGD

    MichaelTGD

    Joined:
    Oct 24, 2019
    Posts:
    16
    A few days ago, VSCode stop recognizing all Unity Namespaces except UnityEngine. I believe the issue came either when I updated Unity, or Unity's VSCode package in the package manager. However, I can not roll either of these back, and check if it solves the issue.
    [Edit: Oops, yes, you can roll them back. And that's what I did to resolve the issue until the VSCode package is updated.]

    This issue ONLY shows up in VSCode. Unity does not report any errors.

    Other scripts and namespaces work fine, and assembly definitions are untouched from before the issue began. Also, I noticed that other packages, such as Odin Inspector, are having the same issue.

    Some quick things to note, to skip some common solutions:
    • Everything is up to date, including VSCode, Unity's VSCode package, and VSCode extensions (Debugger for Unity, C#, etc).
    • My assembly definitions have the correct dependencies. They were attached and everything worked fine until the issue presented itself.
    • I have deleted all .csproj and .sln files, while Unity and Hub were off, and had Unity rebuild them. I have also verified that Preferences>External Tools has VSCode as the editor, and tried regenerating project files.
    • I see the issue in Visual Studio 2022 (preview).
    Things I have tried:
    • Restart/reboot Unity/VSCode/Computer/etc.
    • Changing the editor in Preferences>External Tools, and Regenerating project files for each option.
    • Removing and Re-installing Unity' VSCode package from the package manager.
    • Moving scripts out of assembly definitions and namespaces.
    Thanks for any help!

    Unity 2021.1.20f1
    Unity Package: Visual Studio Code Editor 1.2.4
    VS Code 1.60.1
     

    Attached Files:

    Last edited: Sep 28, 2021
  2. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    Try closing Unity, deleting all the csproj files and your Library and obj folders for your project. Then reopen Unity and select Assets -> Open C# Project to regenerate all those things.

    I don't know how, but sometimes the project gets out of sync with the library files, which breaks Intellisense.
     
    Kan-A-Pesh likes this.
  3. MichaelTGD

    MichaelTGD

    Joined:
    Oct 24, 2019
    Posts:
    16
    Yes, thank you. As I said, I've tried that (many times).

    But what's strange is, it's only with UnityEngine namespaces. Others, such as System, or custom assemblies/namespaces are uneffected.
     
    akuno likes this.
  4. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    I saw you mention restarting Unity/VS Code, messing with your preferences, removing the integration package, and moving scripts. I didn't see you mention actually deleting your csproj files and using "Open C# Project" to regenerate them. It looks like your post was edited shortly before your last response, so maybe you added in more information, I dunno. But in any case, if you're not actually deleting the files I mentioned first, it's possible you're not fully regenerating what you need.

    I've had this issue pop up numerous times in the past, and doing what I said has always fixed it. If it's not working for you, I guess your best bet would be to open a bug report?
     
  5. MichaelTGD

    MichaelTGD

    Joined:
    Oct 24, 2019
    Posts:
    16
    Okay... I'm sorry if you feel my response was rude, and that I edited my post after you responded... I'm not sure what I'd gain from that. The edit was to add spaces between the bullet lists to make them more readable, and less likely to be skipped or skimmed over. I, too, have had the issue you're thinking of many times, and yes, deleting those files fixed that problem, but unfortunately, not in this case.

    But thank you again for offering your help.

    I was hoping someone might know different trick to solve it, or a mistake I might have made somewhere without realizing, that could solve it easily. You might be right; I might need to fill out a bug report. Though with this feeling like a hard to repo bug, I don't hold a lot of hope for that helping.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    My standard blurb: 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.

    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
     
    Zouka48107, Slock20969 and DarndZ like this.
  7. MichaelTGD

    MichaelTGD

    Joined:
    Oct 24, 2019
    Posts:
    16
    Thank you, Kurt, but yes, I also tried these, and to my surprise, they also weren't the solutions.

    HOWEVER, I did manage to finally nail down, and solve, the problem. I doubt this will be the solution to many others who may be dealing with similar issues, but just in case, or to get you thinking about different solutions, I'll mention it here.

    So, as I implied, I was suspecting that Unity's Package for VSCode was likely behind the issue, but I was thinking I was unable to revert to the previous version because it's not possible through the Package Manager. Today, though, I remember that you can actually revert Unity Packages manually, by editing two json files Unity uses to keep track of which packages are in the project. I would caution people from quickly jumping in there and playing around of course, and it probably shouldn't be the first tactic to solve an issue, but if you're mindful and know which packages you need, it's fairly simple.

    The files are
    Packages/manifest.json
    and
    Packages/packages-lock.json
    . Just make sure Unity and Unity Hub are closed. After making the changes and restarting Unity, it will load the listed package version. In my case, I changed the VSCode package in both from 1.2.4 => 1.2.3.

    VScode now recognizes Unity Namespaces, and Intellisense offers suggestions.

    Thank you again to the previous commenters for your help, and good luck to anyone struggling with the issue in the future!
     
  8. ScriptsEngineer

    ScriptsEngineer

    Joined:
    Jun 8, 2018
    Posts:
    37
    Apparently generating the .csproj separately causes the problem in version 1.2.4 because if you uncheck everything as the image below makes everything work normal. (Remembering to delete previous .csproj)
    upload_2021-9-25_10-54-26.png
     
    hassam_794, ujz, hms0589 and 10 others like this.
  9. MichaelTGD

    MichaelTGD

    Joined:
    Oct 24, 2019
    Posts:
    16
    Oh, interesting. I might give it a try later. Also, I saw Unity updated two versions since I first posted, I wondered if that had an effect with the package.

    I have to admit, I don't know enough about csprojs. Is there a disadvantage to this approach?
     
  10. ScriptsEngineer

    ScriptsEngineer

    Joined:
    Jun 8, 2018
    Posts:
    37
    MichaelTGD likes this.
  11. MichaelTGD

    MichaelTGD

    Joined:
    Oct 24, 2019
    Posts:
    16
    Ah, yes, that does indeed seem to be the issue. Thanks for pointing that out, I'll keep an eye on the issues there to watch for a fix, as it's still an issue with 2021.1.22f1. You're previous solution does line up with what they're reporting in the other issue (which is actually the same issue).

    https://github.com/Unity-Technologies/com.unity.ide.vscode/issues/4
     
    viridisamor likes this.
  12. viridisamor

    viridisamor

    Joined:
    Mar 1, 2019
    Posts:
    3
    My issue today has been: VS Code suddenly doesn't recognize classes from the Tilemap Extras package, and wholly doesn't recognize the InputSystem namespace. Out of no where, when they were working fine before today. I'm supposing I may have updated something, but I can't remember.

    I've spent the whole day trying everything I could find, trying old tricks, and this was the last thing I wanted to try but have never had to downgrade a package, so I didn't know how. The docs are confusing about it. I get that people probably shouldn't be messing with the manifest files, but jeez... There really ought to be an option in the editor to downgrade packages because of cases like this. Thank you for sharing this!

    Issues like this should be easier to find, but at least now I know where to look next time (as it happens too often for me). And thank you for the link!
     
    MichaelTGD and ScriptsEngineer like this.
  13. Gawidev

    Gawidev

    Joined:
    Jan 28, 2021
    Posts:
    8
    Ive been having this problem for ages, specifically with UnityEngine.InputSystem and TMPro. Somehow just unchecking everything and clicking the regenerate files button fixed it (without having deleted my previous cproj files). Just posting in case this works for someone else
     
    ujz, hms0589 and VrTechEx like this.
  14. RogueStargun

    RogueStargun

    Joined:
    Aug 5, 2018
    Posts:
    296
    I second GreatGwai. Unchecking everything and regenerating files (without deleting the previous csproj) works.
     
    Gawidev likes this.
  15. VrTechEx

    VrTechEx

    Joined:
    Aug 4, 2013
    Posts:
    40
    As GreatGwai said, it works by unchecking everything, and regenerating files.
     
    Gawidev likes this.
  16. Slock20969

    Slock20969

    Joined:
    Nov 18, 2015
    Posts:
    15
    Almost a year later, but updating the VS Code Editor allowed it to see NewtonSoft JSON. It was a minor revision too, so anything might help.
     
    Gawidev likes this.
  17. danielesuppo

    danielesuppo

    Joined:
    Oct 20, 2015
    Posts:
    331
    I'm trying to use Nethereum plugin, and the Namespace is not recognized.
    - I've updated VsCode Unity plugin
    - I've cleaned all the .csproj files
    - I've regenerated the project (with everything disabled)
    Nothing...

    But in Visual Studio it's recognized
     
  18. ujz

    ujz

    Joined:
    Feb 17, 2020
    Posts:
    29
    Just adding that this solution works for anyone struggling to get UnityEngine.Animations.Rigging working. It is the only namespace that didn't work for me. I struggled for several hours trying various things (deleting .csproj, regenerating project files, reimporting packages, updating VSCode, creating Assembly Definitions, etc).

    In the end, I came across this thread, updated my Unity package for Visual Studio Code Editor to version 1.2.5, unchecked everything as per above, and it finally worked.
     
  19. sarahpuspdew

    sarahpuspdew

    Joined:
    Nov 7, 2021
    Posts:
    4
    I used the 1.2.5 as well and my intellisense works perfectly fine but not with methods such as Start(), OnCollisionEnter(), etc. Do you perhaps know why?
     
  20. sebglon

    sebglon

    Joined:
    Mar 31, 2022
    Posts:
    5
    I have the same issue with visual studio editor 2.0.22
     
  21. MrW82

    MrW82

    Joined:
    Feb 2, 2022
    Posts:
    8
    <Using Unity 2020.3.25f>
    I had similar issue where opening a script file by clicking on it would open Visual Studio without intellisense recognizing the UnityEngine parts.

    MY SOLUTION - Just opening it by the "Assets - Open C# Project" instead of doubleclicking a cs file fixed my problem.