Search Unity

Missing Namespace error in VS but game compiles and runs just fine

Discussion in 'Editor & General Support' started by Seanchaoz, Apr 23, 2022.

  1. Seanchaoz

    Seanchaoz

    Joined:
    Jun 19, 2019
    Posts:
    18
    Short version TL;DR

    In my main Unity project, when copied to a new folder for asset testing, Visual Studio will produce missing namespace references for the new Unity InputSystem and a 3rd party asset - yet the game compiles and runs just fine. The errors are only present in VS and have no impact on the actual project.


    So here's the long version;

    The project is around 2 years old now and has never had any issues. Last week I (foolishly) upgraded it from unity 2020 LTS to the new 2021.3.0f1. After doing this, Visual Studio 2019 Community was posting missing namespace errors. One of them was for the MEC (More Efficient Coroutines) asset, the other was Unity's own InputSystem (the new one). VS claimed they did not exist, despite the fact that they did exist, and they were working perfectly fine because the game would compile and play with no issues. If they really were missing or broken, the game would not run at all.

    Note: I did back up the original 2020 LTS build prior to upgrading. I initially thought the Unity upgrade had broken the project somehow - but that is not the case. I'll get back to that a bit later.

    Needless to say, a lengthy process began to try and fix those 'fake' errors, including regenerating project files, building and rebuilding solutions, uninstalling and re-installing both the MEC and the InputSystem, deleting the Library folder, etc. etc.. I also upgraded to Visual Studio 2022 and for the most part everything was unsuccessful, but somehow at some point, with the help of some discord friendlies, we got the errors fixed and all seemed right. I cannot even remember how we did it.

    Now, fast forward a few days and I decided to test out some new assets by copying my existing asset folder to a new one and changing its name to Asset Testing, because I prefer to not install new assets into my main project until I'm certain I want to actually use them. I've done this before, both with the project in question and with other projects and it has never been a problem. But now the new copy of the project was back to doing the missing namespace errors in VS for the MEC and InputSystem - again, the game compiled and ran just fine, they were "false positive" errors.

    Over the past 2 days I've done pretty extensive trial and error testing, creating multiple copies of the project and putting them in various differently named folders. Some times they would work, but then I would delete one and magically all the others then "broke". I've been back and forth trying to look for patterns and trying to find out why sometimes copies work and why not, and so far nothing is making sense.

    The only version of the project that is consistently working is the "fixed" original I mentioned earlier.

    Now here's the real head scratcher. Today I tried making a copy of the original Working Project. Let's call it the Copy Project.

    The Copy Project has fake MEC and InputSystem errors in VS. It compiles and runs just fine in Unity, nothing is broken. I then zipped the Copy Project and sent it to a 2nd PC - a system that has never had Unity or VS installed. I installed Unity and Visual Studio and ran the Copy Project on the 2nd PC - everything works. I even tried making a copy of the Copy Project (still on the 2nd PC), and the copy works flawlessly there as well. So I zipped up the copy of the Copy Project from the 2nd PC, sent it back over to my main PC, ran it and... errors. I even sent the Copy Project to a friend on Discord who tried it out and it ran perfectly fine on his system.

    So whatever is happening seems to be localized to my main PC. It seems as though Visual Studio is unable to properly process folder/file structures outside of some very specific instances. Almost as if it has cached references that get messed up when the original project is copied to new folder names - but only sometimes, not all the time! It's so confusing. I've at one point managed to have 3 out of 4 project copies working without errors, but deleting the one "broken" project then broke the other two, leaving me again with only the original project folder in working order.

    I said I'd get back to the original 2020 LTS backup version - I tried running it and it works fine. But when I make copies of it, even running in the 2020 LTS version, the same fake MEC and InputSystem errors in VS prop up. This is why I believe the update has not broken the project, but something has definitely gone horribly wrong. It also seems to only apply to this one specific project. I have other smaller game projects, also using MEC and InputSystem, and I can merrily make copies of them all day and they work just fine.

    I've tried so many different things that I've lost track of what's going on anymore.

    Any suggestions?
     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,043
    Close unity
    Go to the project folder
    Delete all csproj and sln files (I also delete library sometimes to clean up cached stuff from prev unity versions, but that shouldn't change anything for this issue)
    Open the project again
    Go to preferences -> 3rd party tools
    Generate the proj files
    Open a script and let it load
     
  3. Seanchaoz

    Seanchaoz

    Joined:
    Jun 19, 2019
    Posts:
    18
    Gave it a try again (tried it multiple times already) but it makes no difference. Followed the exact instructions but VS still complains about missing namespace, despite them not missing and the project working.

    I even tried deleting the cached VSfiles in the Obj/Debug folder, as well as the hidden .vs folder. No luck.
     
  4. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,043
    Did you check all the boxes for regenerating the files?
     
  5. Seanchaoz

    Seanchaoz

    Joined:
    Jun 19, 2019
    Posts:
    18
    I did, yes. Tried checking/unchecking various ones in various combinations.

    I will do a windows reinstall eventually and see if that doesn't solve it. I can't really imagine it being anything other than something fundamentally corrupted somewhere either in VS or Unity or some NET stuff, I don't really know. All my projects work perfectly fine on other PCs - copy/paste/rename project folders all day long and nothing breaks. Do it once on this PC and VS flips out immediately.
     
  6. ujz

    ujz

    Joined:
    Feb 17, 2020
    Posts:
    29
    I had a similar issue. UnityEngine.Animations.Rigging was not recognized by VS but the game still compiled fine without any errors at all. (And Animation Rigging worked fine.)

    Eventually I came across this thread which pointed out that there was a specific bug with csproj files in the VSCode package version 1.2.4. So I updated my Unity package for Visual Studio Code Editor to version 1.2.5, unchecked all boxes and regenerated project files as described in that thread, and it finally worked. Just leaving here for future reference.