Null scene component initialized in C++ constructor

Hi everyone,

I am encountering an error when trying to use a scene component that I have initialized in the C++ constructor for my class.

Here is the header.h file with relevant declarations

//FPS mesh component, visible only to the owning player
	UPROPERTY(VisibleDefaultsOnly, Category = Mesh)
		USkeletalMeshComponent* FPSMesh;
UPROPERTY(VisibleDefaultsOnly)
		USceneComponent* FPSMuzzleComponent;
UPROPERTY(VisibleDefaultsOnly)
		USceneComponent* TPMuzzleComponent;

Here is the relevant constructor code in the cpp file (there is more, ask if you want to see it):

Afpscharacter::Afpscharacter()
{
  FPSMesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("FirstPersonMesh"));
  
  check(FPSMesh != nullptr);
FPSMesh->SetOnlyOwnerSee(true);
FPSMesh->bCastDynamicShadow = false;
FPSMesh->CastShadow = false;
//Hides third person mesh from owner
	GetMesh()->SetOwnerNoSee(true);
FPSMuzzleComponent = CreateDefaultSubobject<USceneComponent>(TEXT("Gun Muzzle"));
FPSMuzzleComponent->SetupAttachment(FPSMesh);
check(FPSMuzzleComponent != nullptr);
}

And here is the function where the muzzle is null

void Afpscharacter::PositionAndAttachGunInFP(FWeaponDataStruct GunToEquip)
{
if (FPSMuzzleComponent == nullptr)
	{
		UE_LOG(LogTemp, Warning, TEXT("NULL ERROR ALERT"));
	}
	UE_LOG(LogTemp, Warning, TEXT("name = %s"), *FPSMuzzleComponent->GetFName().ToString());
	UE_LOG(LogTemp, Warning, TEXT("name2 = %s"), *FPSMuzzleComponent->GetAttachmentRootActor()->GetFName().ToString());
}

The program fails after outputting NULL ERROR ALERT, as I can see in the log file (meaning the check assertion passes in the constructor). I am confused as to why the muzzle component is becoming null. This function is running on clients only, so I was thinking that maybe the constructor is only run on the server, but that doesn’t seem right at all. Any help would be appreciated.

If any more info is required please let me know.

Interestingly this same code was used in a previous version, and it worked - it was when I added another scene component (which does not interact with the FPS one) that caused the errors.

Hi DanZeDev,

Move the code from your constructor to the “OnConstruction(const FTransform &Transform)” method.

1 Like

After doing this, I get the error

Fatal error: [File:D:/Build/++UE4/Sync/Engine/Source/Runtime/CoreUObject/Private/UObject/Obj.cpp] [Line: 113] No object initializer found during construction.

which causes a crash - This occurred on the first line, FPSMesh = CreateDefaultSubObject…

I think it is due to the use of this function when the function is only meant for constructor use - if I use NewObject and register, the components don’t appear in BP, which I need. Any suggestions?

Is Afpscharacter inheriting from ACharacter? If so, make sure to attach your FPSMesh to Root component. If not, make sure you attach it somewhere, or make it the root component if you want that.

If that doesn’t fix it, I would recommend reparenting your blueprint to actor, then saving, and then reparenting again to Afpscharacter. Sometimes blueprints can get corrupted like that, I’ve seen it in the past a few times

2 Likes

Hi Zeaf,

Afpscharacter does inherit from Acharacter.

In the other constructor code not posted, I have set the root component (from following the first person shooter tutorials and stuff).

Would this blueprint change be in addition to moving the code to OnConstruction, as suggested by Recourse Design, or can I keep the code in the original constructor? I’ve heard of the BP bug you describe, but haven’t tried that fix yet, so once i get back I will post an update.

Thank you both for the help so far.

I don’t think you should create default subobjects like that outside the constructor, in fact I don’t think it’s even possible, so I wouldn’t move it to on custruction. Just make sure you attach the fpsmesh component to something, otherwise things can get weird, so just do FPSMesh->SetupAttachment(RootComponent) in addition to everything else that you do

The reparenting usually fixes any weird serialization that might have happened with your components, especially in your case where the FPSMesh skeletal component was not attached to anything, but you will lose any potential modified properties in some of your components

1 Like

When using OnConstruct replace createdefaultsubobject with NewObject

void Afpscharacter::OnConstruction(const FTransform &Transform)
{
FPSMuzzleComponent = NewObject<USceneComponent>(this); // where this is outer component
FPSMuzzleComponent->RegisterComonent();

FPSMuzzleComponent->AttachToComponentComponent(FPSMesh);

//..... fill in rest of constructor

}
3 Likes

Thank you so much for the reparenting suggestion, it fixed the problem! The BP settings have all reset but that’s fine, better than running around in circles trying to fix my code (which for once didn’t seem to be the problem).

Thank you 3DRaven for this useful advice for anyone else who didn’t understand OnConstruction