Here’s a common error that people usually run into when doing Unity scripting, and it’s one that even reasonably-skilled programmers can take awhile to fix.
That being said, the fix is actually quite straightforward, because what the Console is trying to tell you here is simply that you have 2 copies of the same class.
There are 2 ways this can happen. You can either watch the video below, or scroll on further if you prefer to read about the issue.
Scenario #1: You have duplicate scripts
Whenever you see this error in your Console, your first order of business should be to head to your Project window, and do a search for whichever script is causing you the problem. 90% of the time, it’s 2 scripts with the file name, and containing the same class name.
This should reveal more than 1 copy of the offending script. To fix this, you’ll want to delete or rename one of the script file’s name, and the class definition.
If you absolutely need both classes to use the same name, you can add a unique namespace to each of your duplicates, so that they can coexist.
PlayerController.cs
using UnityEngine;
namespace AwesomeGame {
public class PlayerController : MonoBehaviour {
// The rest of your class goes here.
}
}
This means that you’ll have to access the namespaced class(es) by their namespaces and class names.
void Start() {
// Accessing our other PlayerController from another script.
AwesomeGame.PlayerController pc = FindObjectByType<AwesomeGame.PlayerController>();
}
You can also make use of the using
keyword to import the namespace into your scope of your other scripts, so you don’t need to prefix the namespace, but this means that you lose access to the un-namespaced (i.e. global) PlayerController
.
using UnityEngine; using AwesomeGame; // Specifying we are using the AwesomeGame namespace. public class CameraController : MonoBehaviour { // The rest of your class goes here. void Start() { // Getting AwesomeGame.PlayerController. PlayerController pc = FindObjectByType<PlayerController>(); // Getting the global (un-namespaced) PlayerController. global::PlayerController gpc = FindObjectByType<global::PlayerController>() } }
Scenario #2: You have nested classes
If you’ve tried to find duplicate script files to no avail, then the problem might be that you have nested classes that are — again — duplicates. Nested classes are classes inside your script files, declared alongside the main class in the file, and they look something like this:
PlayerController.cs
using UnityEngine public class PlayerController : MonoBehaviour { void Start() { // Do stuff } } // Nested class. class CameraController { void Start() { // Do stuff. } }
Finding the nested classes will be a bit more challenging. You won’t be able to do a search on the Project window this time. Rather, we will be using the command line to do a string search.
- Go to your Assets folder in the Project window.
- Right-click on the Project window, and select Show in Explorer (or Show in Finder for Mac).
- Your Assets folder should appear in your File Explorer or Finder. You’ll need to open Command Prompt or Powershell (Windows) or Terminal (Mac) in that folder. For Windows, hold Shift + Right-click; for Mac, go to Services > New Terminal at Folder.
- Enter either one of the commands below (replace
CameraController
below with your own offending class), and your command line should output all occurrences of the class. Now, you can either delete the duplicate classes, or namespace them (refer to above example).
For Windows
> findstr /s /c:"CameraController" *.cs
For Mac
$ grep -r --include \*.cs "CameraController" ./
Conclusion
Every global namespace error you get should either be scenario #1, scenario #2, or some form of permutation of both of them. Once you eliminate the duplicate classes, the error(s) will go away and you can continue with your work.
Very big thanks!