SpawnSystemAttached + RootComponent + C++ = Problem

I’m spawning a niagara particle system.
And I think I have a problem with the RootComponent.

When I do it in blueprint everything works fine.

When I do it in C++ the system doesn’t spawn

I have tried to do it at location instead of attached and it also works correctly.

So my conclusion is that there must be a problem with the Root component… Or maybe the SpawnSystemAttached() function is failing…

So these are the components that my actor has.

5

Anybody knows what could be the problem?

Thank you so much!!

The problem seems to be the attach function:
The problem is exactly the same if it is done in this other way.

So I think there must be some problem with attaching a UNiagaraComponent to a USceneComponent.

Must be a problem that blueprints solve under the hood.

Does anyone know how to do it?

Implementation for actor MyActor. Don’t forget to replace YOUR_API with own.

Header

	// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyActor.generated.h"

UCLASS()
class YOUR_API AMyActor : public AActor
{
	GENERATED_BODY()
	
public:	

	AMyActor();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	

	virtual void Tick(float DeltaTime) override;


	UPROPERTY()
	class UAudioComponent* ShootingAudioComponent;

	UPROPERTY()
	class UAudioComponent* ImpactAudioComponent;
	
	UPROPERTY(EditAnywhere, BlueprintReadWrite)		
	TObjectPtr<class UNiagaraSystem> NiagaraShootingStar;

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	class UNiagaraComponent* ShootingStarNiagaraSystem;
	
};

cpp

#include "MyActor.h"
#include "Components/AudioComponent.h"
#include "NiagaraFunctionLibrary.h" 
#include "NiagaraSystem.h" 
#include "NiagaraComponent.h" 


AMyActor::AMyActor()
{

	PrimaryActorTick.bCanEverTick = true;
	

	ShootingAudioComponent = CreateDefaultSubobject<UAudioComponent>(TEXT("Shooting Audio"));
	SetRootComponent(ShootingAudioComponent);

	ImpactAudioComponent = CreateDefaultSubobject<UAudioComponent>(TEXT("Impact Audio"));
	ImpactAudioComponent->SetupAttachment(ShootingAudioComponent);

	ShootingStarNiagaraSystem = CreateDefaultSubobject<UNiagaraComponent>(TEXT("UNiagara Component Shooting Star"));
	ShootingStarNiagaraSystem->SetupAttachment(ShootingAudioComponent);
}

// Called when the game starts or when spawned
void AMyActor::BeginPlay()
{
	Super::BeginPlay();
	
	FVector Location = GetActorLocation();
	FRotator Rotation = GetActorRotation();

	ShootingStarNiagaraSystem = UNiagaraFunctionLibrary::SpawnSystemAttached(NiagaraShootingStar,
		GetRootComponent(), "", Location, Rotation, EAttachLocation::KeepRelativeOffset, true, true,
		ENCPoolMethod::AutoRelease, true);

}

// Called every frame
void AMyActor::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}
1 Like

Thank you very much for your help.

1 Like

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