Root Component in Blueprint still DefaultSceneRoot after assigned RootComponent in C++

Hello, I am new to Unreal here. I try to learn and replicate the projectile in first-person shooter. I make USphereComponent in .h and set as root component in .cpp as follows:

MyProjectile.h:

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	UPROPERTY(VisibleDefaultsOnly, Category = Projectile)
		class USphereComponent* CollisionSphere;

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Movement)
		class UProjectileMovementComponent* ProjectileMovement;

	UFUNCTION()
		void OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& Hit);

	UPROPERTY(EditAnywhere)
		float DamageValue = 20.0;

MyProjectile.cpp:


#include "MyProjectile.h"

#include "Components/SphereComponent.h"
#include "GameFramework/ProjectileMovementComponent.h"

// Sets default values
AMyProjectile::AMyProjectile()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you dont need it.

	PrimaryActorTick.bCanEverTick = true;

	CollisionSphere = CreateDefaultSubobject<USphereComponent>(TEXT("Sphere Collision"));
	CollisionSphere->InitSphereRadius(20.0f);
	RootComponent = CollisionSphere;

	ProjectileMovement = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("Projectile Movement"));
	ProjectileMovement->UpdatedComponent = CollisionSphere;
	ProjectileMovement->InitialSpeed = 3000.0f;
	ProjectileMovement->MaxSpeed = 3000.0f;
	ProjectileMovement->bRotationFollowsVelocity = true;
	ProjectileMovement->bShouldBounce = true;

	InitialLifeSpan = 3.0f;

}

However, after I create blueprint from this class. The root component still shown as “DefaultSceneRoot”.

I already doubled-check from tutorial but I am not sure what I am missing. I use Unreal 5.1 followed tutorial from Unreal Engine & C++ Tutorial - 1st Person Shooter Game (youtube.com).

If you want any information, feel free to ask. Thanks!

on MyProject.cpp you need to do SetRootComponent(CollisionSphere) instead of just assigning it.

though your actual problem is likely the uproperty definition

UPROPERTY(BlueprintReadOnly, VisibleDefaultsOnly, Category = Projectile)
		class USphereComponent* CollisionSphere = nullptr;

add the BPReadOnly and try that.

Thank you.

1 Like

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