Renaming c# classes and Unity…

As far as I know, there is no ‘correct’ way to rename a class that you’ve built inside a Unity C# file with ease and 0 pain…  but there is certainly a wrong way to do it:

Approach it in this order:

Rename the ????.cs file using the Unity Editor.  Click on the file in the project explorer and use what ever method you’d like to rename it to the new class name.  This means that Unity will keep a reference to the newly named file and any instances of that class file attached to objects in the scene will get updated.

Now, open the file in your text editor (Mono, VS, whatever) and use ‘Refactor or Rename’ on the actual class name and let the editor change all references in the project.  Save all the changed files and you should be able to build just fine.

Go back to Unity and the project should build and run without any errors.

If you do what I did – rename the class first in your text editor, then you’ll end up replacing every ‘missing script’ error by hand – not so much fun — you’ve been warned!

Leave a Reply