How to Add a C++ Class in Unreal Engine the Right Way
Add C++ classes to an Unreal project without the usual mistakes: wrong folder, missing prefix, .generated.h errors, wrong API macro, and renaming later.
In this article
Adding a C++ class to an Unreal project sounds trivial, but it is where a surprising number of beginner errors come from: classes that never appear in the editor, .generated.h errors, linker errors and Blueprints that break after a rename. Almost all of them come from a handful of rules that Unreal's build tools expect you to follow.
The safest way: let the editor do it
In the editor, choose Tools → New C++ Class, pick a parent class, name it and choose Public or Private (or keep the default). Unreal writes the .h and .cpp into the right folders, regenerates project files and compiles.
This also works in a Blueprint-only project: the first C++ class creates the Source folder, a game module and the project files. You need a C++ compiler installed (Visual Studio on Windows, Xcode on macOS).
Adding files by hand
Many developers prefer to create files themselves, especially when adding several classes at once. That is fine as long as the files end up where Unreal looks:
MyGame/
MyGame.uproject
Source/
MyGame/
MyGame.Build.cs
Public/
Inventory/InventoryComponent.h
Private/
Inventory/InventoryComponent.cpp
Headers go in Public, source files in Private, with the same subfolders in both. Some projects use a flat layout with both files side by side; that works too, as long as both are inside Source/<Module>.
Then:
- Close the editor.
- Right-click the
.uprojectand choose Generate Visual Studio project files (or refresh the project in Rider). - Build the Development Editor configuration in your IDE and open the editor again.
Live Coding is great for changing code inside existing functions, but for new classes a full build with the editor closed is the most reliable path.
Do not use Visual Studio's "Add New Item"
This is the classic trap. In an Unreal solution, Visual Studio's Add → New Item dialog suggests a folder under Intermediate/ProjectFiles by default. Files saved there are not part of your module, so Unreal never compiles them and they never appear in the editor. If you use the dialog, change the location to your Source/<Module>/... folder every time, or create files another way.
The rules that break builds
1. Prefix in the class name, not in the file name
| Parent | Prefix | Class | Files |
|---|---|---|---|
| Actor, Character, Pawn | A | ADoor | Door.h, Door.cpp |
| Other UObjects (components, subsystems, data assets) | U | UInventoryComponent | InventoryComponent.h |
| Structs | F | FItemRow | ItemRow.h |
| Interfaces | U + I | UInteractable, IInteractable | Interactable.h |
Unreal Header Tool checks these prefixes and stops with an error if they are wrong.
2. .generated.h matches the file and comes last
#pragma once #include "CoreMinimal.h" #include "Components/ActorComponent.h" #include "InventoryComponent.generated.h" // always the last include
The name must match the header's file name. Add an include below it and you get "#include found after .generated.h file - the .generated.h file should always be the last #include in a header". Rename the file without updating this line and the include cannot be found.
3. Use your own module's API macro
class MYGAME_API UInventoryComponent : public UActorComponent
The macro is your module name in capitals plus _API. Copy a class from another project or plugin and forget to change it, and you get linker errors. Linker errors like unresolved external symbol can also mean a module is missing from PublicDependencyModuleNames in your Build.cs, for example GameplayTags or EnhancedInput.
4. GENERATED_BODY() first
GENERATED_BODY() must be the first thing inside the class or struct. Unreal inserts its reflection code there.
5. Turn off Tick if you do not need it
The editor's Actor and Component templates enable ticking (bCanEverTick = true). Most gameplay code reacts to events, timers or input instead. Set it to false and remove the Tick function if you do not use it.
Renaming or deleting a class later
Blueprints and levels save a reference to the C++ class by its path, like /Script/MyGame.OldName. Rename the class and those assets can no longer find it. Add a class redirect in Config/DefaultEngine.ini:
[CoreRedirects] +ClassRedirects=(OldName="/Script/MyGame.OldName",NewName="/Script/MyGame.NewName")
Then open and resave the affected assets. To delete a class, first make sure no asset uses it, then close the editor, delete the files, regenerate project files and rebuild.
Generate the files instead
Copying an old class and renaming it is how many of these errors start. The free Unreal C++ Class Generator writes the .h and .cpp for an Actor, Character, Pawn, Component, Subsystem, Data Asset, Interface, Blueprint Function Library or Struct, with the right prefix, includes, API macro and folder paths, and Tick off unless you ask for it. To make the new class's properties editable, the Specifier Builder and UPROPERTY Specifiers Explained cover the macros.
Coming from Unity? Porting a Unity Project to Unreal walks through moving code, assets and habits over, from GameObjects to Data Assets.
Summary
- Use Tools → New C++ Class, or put files in
Source/<Module>/PublicandPrivateyourself. - Never save new files to
Intermediate(Visual Studio's default location). - Regenerate project files and do a full build after adding classes.
- Prefix in the class name,
.generated.hlast, your own_APImacro,GENERATED_BODY()first. - Rename classes with Core Redirects so assets keep working.
Frequently asked questions
Why does my new Unreal C++ class not compile or show up?
Usually because the files are not inside your module's Source folder (Visual Studio's Add New Item saves to Intermediate/ProjectFiles), project files were not regenerated, or the editor was not rebuilt. Move the files to Source/<Module>/Public and Private, regenerate project files and build from your IDE.
What does 'the .generated.h file should always be the last #include' mean?
Unreal Header Tool requires #include "Name.generated.h" to be the last include in the header. Move any include below it above it.
How do I rename a C++ class in Unreal without breaking Blueprints?
Rename the class and files, then add a class redirect under [CoreRedirects] in Config/DefaultEngine.ini, for example +ClassRedirects=(OldName="/Script/MyGame.OldName",NewName="/Script/MyGame.NewName"). Assets that referenced the old class then load with the new one.
Can I add a C++ class to a Blueprint-only project?
Yes. Use Tools → New C++ Class in the editor. Unreal creates the Source folder, a game module and the project files for you. You need a C++ compiler such as Visual Studio installed.