error CS0101: The namespace already contains a definition for 'X'
error CS0101: The namespace '<global namespace>' already contains a definition for 'X'
Two classes with the same name exist in the same namespace. Unity compiles every script in Assets together, so a second PlayerController anywhere in the project, even in a folder you forgot about, collides with the first.
Read the Console line first
The message names the class, and the file and line point at one of the two declarations. Search the Project window for the class name to find the other.
Common causes
The same script was imported twice
Downloading a script again or importing a package twice can leave PlayerController.cs and PlayerController 1.cs side by side. Both declare the same class.
An imported asset uses the same class name
Asset Store packages and tutorials often use common names such as GameManager, AudioManager or Singleton. If yours has the same name in the global namespace, the two collide.
A backup copy is still inside Assets
A copied folder such as Assets/Scripts_old is still compiled. Move backups outside the Assets folder or rely on version control instead.
The fix
namespace MyGame
{
public class GameManager : MonoBehaviour
{
// Your class no longer collides with another GameManager
// declared in the global namespace or in a different one.
}
}Stop it coming back
Put your own scripts in a namespace named after your game, and keep backups out of the Assets folder. When you import an asset, check whether it ships classes with the same names as yours before you write code against it.
Questions
- How do I find the duplicate class quickly?
- Search the Project window for the class name, or search the whole project folder in your IDE for
class GameManager. Check Packages too: a local package can define it. - Can two classes share a name if they are in different folders?
- Not in the same namespace. Folders do not separate code; namespaces and assembly definitions do. Two classes named GameManager can coexist in different namespaces.
- Is it safe to delete the duplicate file?
- Compare the two first: if they differ, keep the newer or the one your scenes use. Delete the copy together with its .meta file from inside Unity so the right GUID survives.
Scripts that avoid it
Related errors
Got a different error? Paste it into the error lookup.