Why can't Unity or Visual Studio allow you to duplicate a behavior

Of course it would a new name and change its class but when I have a working but not neat code I would like to copy it to make a better version which takes work to get functional.

AFAIK there is no automatic method to duplicate and rename the file/class in one go, but you can easily do what you want in Unity by just selecting script in the Project Browser, use CTRL-D to duplicate.

Obviously you have to rename the file as it will just be the same name with a number appended and also change the class name, but it does what you want. Though I would advise CTRL-D, open file in visual studio, rename the class then rename the file using the solution explore or Unity Project Explorer as that way you will only incur Unity recompiling all scripts twice instead of three times.

Alternatively in Visual Studio in Solution Explorer you can select the script, drag and hold CTRL to duplicate it. Again you’ll have to rename the file and class. This gets around the issue of Unity recompiling scripts until you are ready and return to Unity, however I’m unsure if this would cause any issues down the line until you’ve forced Unity to recompile, such as missing references or not being able to reference this new class from an existing class in visual studio.

1 Like

I turned off autocompile just now by unclicking Refresh- I didn’t that’s what Unity meant by it.

Because it is not a very common operation.

You can duplicate a class with Ctrl+D and then renaming it by hand. Or you can copy/paste it into another file. Given that in C# functions are contained with the class, this doesn’t take long.

So, just to address that original reason you want such a thing: poor man’s version control. Unless you need both behaviors in the game at the same time, you shouldn’t keep using duplicate-and-rename to keep backup old versions “safe” while making a new improved version. You should look into using a proper version control system. Most are free, and they can help you review the changes each time you decide to commit or lock in improvements, or to roll back to older versions of one or more files all together.

2 Likes

I beat the “use version control” drum myself, but is it really an answer here? It’s not unusual to develop a new system or component while the predecessor is still in use, and that feels pretty independent to whether or not you’re using version control.

Duplicate-and-rename does seem both trivial enough and rare enough to not need to take up space in the UI, though.

Agreed, I most often find myself in this situation when I’m testing different algorithms to achieve the same thing based on some criteria ( e.g. minimise memory use vs performance ). What I tend to do is put the new version into its own namespace, a variation of the original namespace. That way I can switch between them by simply changing namespaces in other classes or via alias.

Alternatively i’d use a factory system if i need multiple versions of the algorithms accessible at the same time say to choose at runtime.