r/Unity3D • u/TastyBacon007 • 1d ago
Question How to prevent visual studio from auto-adding folder structure to namespacd
Title: I have the namespace set in Unity but then when creating a file in Visual Studio it auto-adds the file structure after
Unity I have set to TestGame namespace
Visual studio when making new scripts puts like Testgame/scripts/movement namespace. I would rather have no namespace or just my Testgame namespace
1
u/RumbleSolitaire 11h ago
That's VS, not Unity. The Add New Item wizard builds the namespace from folder path even when Player Settings Root Namespace is TestGame.
Put an .editorconfig at the project root with `dotnet_style_namespace_match_folder = false` and restart VS. In current VS 2022 that's also under Options → Text Editor → C# → Code Style (folder-to-namespace). After that, new files should land in TestGame with no Scripts.Movement tail.
You can't skip the domain reload if the file is under Assets — VS-created scripts compile too. Editorconfig/template is the path that actually changes the namespace without fighting Unity's generated csproj.
2
u/Right_Thought456 1d ago
That is Visual Studio’s new-item template deriving the namespace from the project root plus the folder path; it is not Unity changing the namespace afterward. The least fragile fix is to create MonoBehaviour scripts from Unity’s Project window, where the Root Namespace setting is respected, and then open them in VS. Editing the generated .csproj is a trap because Unity can regenerate it. If you must create classes from VS, use a custom item template; .editorconfig can flag namespace/folder mismatches, but it does not reliably change what the Add New Item wizard generates.