UAudioComponent NULL after initialization?

So i want to play an audio file, which as always worked.
I have a UAudioComponent, and a USoundCue.

UPROPERTY(EditAnywhere)
		USoundCue* pointSound;
	UPROPERTY(EditAnywhere)
	UAudioComponent* pointSoundComponent;

I set the sound of the USoundCue in a blueprint, and that works.
In the constructor i initialize the UAudioComponent like this

pointSoundComponent = CreateDefaultSubobject<UAudioComponent>(
	TEXT("point")
	);
pointSoundComponent->bAutoActivate = false;

After that i do backgroundMusicComponent->SetSound() with the USoundCue as parameter (which is not null) in the beginplay function. But there the component is back NULL again.
The weird thing is that this worked at first, but out of nowhere it doesnt anymore. When the begin play starts, it’s back NULL again so i can’t assign a sound to it anymore.

constructor looks like this

AFlappyBird::AFlappyBird()
{
	PrimaryActorTick.bCanEverTick = true;

	pointSoundComponent = CreateDefaultSubobject<UAudioComponent>(
		TEXT("point")
		);
	pointSoundComponent->bAutoActivate = false;

	flappyBirdMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("flappyBirdMesh"));
	flappyBirdMesh->SetupAttachment(RootComponent);

	movementComp = CreateDefaultSubobject<UFloatingPawnMovement>("MovementComp");

	SideViewCameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("SideViewCameraComp"));
	SideViewCameraComponent->bUsePawnControlRotation = false; 

	thirdPersonViewCameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("thirdPersonViewCameraComponent"));
	thirdPersonViewCameraComponent->bUsePawnControlRotation = false; 

	flappyBirdMesh->SetRelativeLocation(FVector(0, 0, 0));
}

Begin play looks like this

void AFlappyBird::BeginPlay()
{
	Super::BeginPlay();
	
	pointSoundComponent->SetSound(pointSound);

	SetActorLocation(FVector(-10., -2516,  4200));
	SetActorRotation(FRotator(0,90,0));
	SideViewCameraComponent->SetWorldRotation(FRotator(0, 0, 0));
	SideViewCameraComponent->SetWorldLocation(FVector(-3060.000000, -1380.000000,  2650.000000));
	thirdPersonViewCameraComponent->SetRelativeRotation(FRotator(0,90,0));
	thirdPersonViewCameraComponent->SetRelativeLocation(FVector(-12, 0, 1.5));

	SideViewCameraComponent->SetActive(true);
	thirdPersonViewCameraComponent->SetActive(false);

	
}

Does anyone have any idea why this may happen?

i did now :slight_smile:

Could you post more of the surrounding code. Your initialization looks correct

Ok so a couple things.

  1. I believe components are usually VisibleAnywhere and BlueprintReadOnly by convention.

  2. you created the object but never setup the attachment to your actor so I’m guess what’s happening is that it’s getting caught in the garbage collection after the constructor. I would just make sure to add a call to setupattachment after you create it.

This actually worked. Now i tried the soundwave class which works perfectly aswell, and requires less initialization.