I have a custom UComponent that inherits off of UPrimitiveComponent. In the header file for this UComponent I have two USphereComponents declared, they both have UPROPERTY headers. I initialize both of these subobjects in the UComponents constructor like so:
Shoulder = ObjectInitializer.CreateDefaultSubobject<USphereComponent>(this, TEXT("Shoulder"));
Shoulder->InitSphereRadius(10.0f);
Shoulder->AttachTo(this);
Shoulder->SetVisibility(true);
Fist = ObjectInitializer.CreateDefaultSubobject<USphereComponent>(this, TEXT("Fist"));
Fist->InitSphereRadius(10.0f);
Fist->AttachTo(this->Shoulder);
Fist->SetVisibility(true);
I have included two of these custom UComponents in a class that inherits from ACharacter. I initialize them in this class’s constructor like so:
RightArmController = ObjectInitializer.CreateDefaultSubobject<UArmControlComponent>(this, TEXT("RightArm"));
RightArmController->AttachTo(GetMesh(), FName("RightShoulder"));
RightArmController->SetMobility(EComponentMobility::Movable);
if (GEngine != nullptr)
{
if (RightArmController->GetShoulder() != nullptr)
{
GEngine->AddOnScreenDebugMessage(0, 400.0f, FColor::Red, "Right arm has shoulder");
}
if (RightArmController->GetFist() != nullptr)
{
GEngine->AddOnScreenDebugMessage(1, 400.0f, FColor::Blue, "Right arm has fist");
}
}
LeftArmController = ObjectInitializer.CreateDefaultSubobject<UArmControlComponent>(this, TEXT("LeftArm"));
LeftArmController->AttachTo(GetMesh(), FName("LeftShoulder"));
LeftArmController->SetMobility(EComponentMobility::Movable);
if (GEngine != nullptr)
{
if (LeftArmController->GetShoulder() != nullptr)
{
GEngine->AddOnScreenDebugMessage(2, 400.0f, FColor::Yellow, "Left arm has shoulder");
}
if (LeftArmController->GetFist() != nullptr)
{
GEngine->AddOnScreenDebugMessage(3, 400.0f, FColor::Cyan, "Left arm has fist");
}
}
At this point when I start a new a game, all four of those messages print to screen, so I know that those components are not null at that point. However, by the time that I reach some similar print statements in Tick, the pointers in RightArmController are null.
Also the components look like this in the Editor:
Any idea what is causing this? Let me know if you need any additional information.