Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Visual Studio creating automatic namespace. How can I avoid that?

Discussion in 'Scripting' started by ProperNovice, Jun 1, 2021.

  1. ProperNovice

    ProperNovice

    Joined:
    Nov 1, 2016
    Posts:
    15
    Hello all.

    I use Solution Explorer in Visual Studio to create new C# MonoBehavior script. It comes with the predefined structure:

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3.  
    4. namespace Assets.Scripts
    5. {
    6.     public class ClassName : MonoBehaviour
    7.     {
    8.  
    9.         // Use this for initialization
    10.         void Start()
    11.         {
    12.  
    13.         }
    14.  
    15.         // Update is called once per frame
    16.         void Update()
    17.         {
    18.  
    19.         }
    20.     }
    21. }
    When I try to create new object of this class, let's say that it doesn't extends MonoBehavior, the autocomplete cannot find it. I have to use it like:

    Code (CSharp):
    1. new Assets.Scripts.ClassName();
    and not just:

    Code (CSharp):
    1. new ClassName();
    Where can I find the option in Visual Studio not to automatically create this part?

    Code (CSharp):
    1. namespace Assets.Scripts
    2. {
    3. }
    A solution would be to create the script within Unity UI. But then Unity compiles my whole code when I focus the window and that is what I want to bypass by doing it internally from Visual Studio itself.

    Thank you
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Nobody does that. I didn't even know that was a thing. Just make the script in Unity. Unity has to create a .meta file for it anyway before you do anything interesting with it.

    When in Rome...
     
    ProperNovice likes this.
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    Nothing prevents you from removing the namespace after making the script, but I'll be honest, no idea where a setting like that would be. I just make the script in Unity and even if it's not a MonoBehaviour script, I just remove the MonoBehaviour from it after creating it.
     
    ProperNovice likes this.
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,992
    ProperNovice likes this.
  5. ProperNovice

    ProperNovice

    Joined:
    Nov 1, 2016
    Posts:
    15
    Thank you guys
     
  6. _geo__

    _geo__

    Joined:
    Feb 26, 2014
    Posts:
    1,115
    Btw. : if auto complete can't find the class then use the suggestion shortcut (I think it's Ctrl + .) It will suggest a solution (adding "using Assets.Scripts;") in this case.
    Don't remove namespaces completely, make them your own. Doing this will avoid a lot of ambiguity errors later on.
     
    amanpu and ProperNovice like this.