"The name is already used by another class" when adding code

So I’m having trouble adding code to my project. For no apparent reason, when I try to add code to my “MyFirst2DGame” project (via File > Add Code to Project), it displays this error:
“Failed to add class GravityTrigger. Failed to automatically hot reload the ‘MyFirst2DGame’ module.”
When I close that message, in the class adding dialog, it shows the error: “The name ‘GravityTrigger’ is already used by another class.” I checked before I tried to add the code that the word “Gravity” never appeared in my project folder.

When I look in Visual Studio, it has indeed added GravityTrigger.h and GravityTrigger.cpp. In the C++ Classes section of the content browser, GravityTrigger is missing. When I added GravityTrigger, the parent class was a TriggerBox. This error doesn’t happen when I try to add GravityTrigger as a child of Actor.

How do I fix this issue so I can add a TriggerBox correctly?

The c++ code resides at the following path ‘Source/[YourProjectName]/Private’ and ‘Public’ respectively. You can remove the files you don’t need (the duplicate named file) from this path and tell UnrealEditor to refresh the visual studio project and it should work fine. Make sure the files you remove are not used by your project (if they were added by mistake, that should be fine).

1 Like

I’ve had this happen before when adding new classes. The files are added correctly and compile fine, but for whatever reason it tries to add them a second time (or gets confused during the hot reload, or maybe because of I’m using Perforce - not sure, haven’t tracked it down yet). Verify that your code builds and then just relaunch the editor.

@eudrea: I don’t have private and public folders, but I do see my code. Though I’m not sure what you’re referring to when you say “duplicate named file”, as there’s only a single .cpp and .h created.

@AdeptStrain: Seems like this works, although at first I had some errors because child classes of TriggerBox are supposed to override some methods, and the .h didn’t specify that, so I had to write in the declarations manually. I wish there was a workaround that wasn’t so messy.