Error: "Default subobject already exists for class"

Hey, bit of a head scratcher for me. I’m following the Ultimate UE5 C++ course on Udemy. I’m doing “Simple Shooter” project from it.

We did some basic stuff, which I (think I) 100% understand:
1 - Created a new C++ class called Gun (from Actor)
2 - Derived a Blueprint class called BP_Gun from it
3 - Assigned 2 components to the constructor so that they show up in the derived BP class.
NOTHING else has been done to the Gun class except these changes:

Gun.h:

private:
    UPROPERTY(VisibleAnywhere, Category = "Components")
    USceneComponent* Root;

    UPROPERTY(VisibleAnywhere, Category = "Components")
    USkeletalMeshComponent* Mesh;

Gun.cpp:

AGun::AGun()
{

    PrimaryActorTick.bCanEverTick = true;

    Root = CreateDefaultSubobject<USceneComponent>(TEXT("Mesh"));
    SetRootComponent(Root);

    Mesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("Mesh"));
    Mesh->SetupAttachment(Root);

}

…and yet when I try to open BP_Gun, I get an error like this: “Default subobject SkeletalMeshComponent Mesh already exists for Gun /Script/SimpleShooter.Default__Gun.”

I tried:
1 - Deleting Intermediate and Binaries folders from the project folder, rebuilding - no success
2 - Step 1 + deleting the temp folders from AppData/Local as well - no success
3 - Switching SkeletalMeshComponent to UStaticMeshComponent - interestingly, I get the same error.

I checked letter-for-letter, my code is exactly the same as the instructors and it works for him. :confused:
What could this be? Help!

Change the ending bit to TEXT("Scene").

This fixed it! I have no idea why, but this fixed it! Can you please explain? :slight_smile:

Simply put, the default subobjects should have unique names

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.