'UE4 crash about attachment rules

The problem occurred when I try to change my default pawn's UPROPERTY value has name "MoveSpeed" in during collision function. Otherwise, there is no problem with that statement, it works really fine. When my default pawn collides with my "rock" pawn, am detecting collision and trying to reduce "MoveSpeed". Here is the code below:

void ARock_1::OnOverlapBegin(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, 
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool FromSweep, const FHitResult& SweepResult)
{
    Aduck* sa = Cast<Aduck>(OtherActor);
    sa->MoveSpeed = 99.f;
}

The sa is my pointer to other actor. So I can modify the pawn on pointer right? In editor when I try to play and test it, editor freezes like 10 sec after that crashes. So I checked the project crush log I saw these lines:

Ensure condition failed: AttachmentRules.LocationRule == EAttachmentRule::KeepRelative && AttachmentRules.RotationRule == EAttachmentRule::KeepRelative && AttachmentRules.ScaleRule == EAttachmentRule::KeepRelative [File:D:/Build/++UE4+Licensee/Sync/Engine/Source/Runtime/Engine/Private/Components/SceneComponent.cpp] [Line: 1812]
AttachToComponent when called from a constructor is only setting up attachment and will always be treated as KeepRelative. Consider calling SetupAttachment directly instead.

In the constructer of "Aduck" class, I attached the components like so:

    if (!RootComponent)
    {
        RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("DuckBase"));
    }
    DuckDirection = CreateDefaultSubobject<UArrowComponent>(TEXT("DuckDirection"));
    DuckDirection->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepWorldTransform);

    DuckSprite = CreateDefaultSubobject<UPaperSpriteComponent>(TEXT("DuckSprite"));
    DuckSprite->AttachToComponent(DuckDirection, FAttachmentTransformRules::KeepWorldTransform);

    USpringArmComponent* SpringArm = CreateDefaultSubobject< USpringArmComponent>(TEXT("SpringArm"));
    SpringArm->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepWorldTransform);

    CameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));
    CameraComponent->AttachToComponent(SpringArm, FAttachmentTransformRules::KeepWorldTransform);

    DikdortgenComponent = CreateDefaultSubobject<UBoxComponent>(TEXT("Ordek"));
    DikdortgenComponent->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepWorldTransform);

My question is what difference between SetupAttachment and AttachToComponent. In which cases I should use them? Or am I doing wrong when I changing MoveSpeed? In documentation of USceneComponent::SetupAttachment it says:

Initializes desired Attach Parent and SocketName to be attached to when the component is registered. Generally intended to be called from its Owning Actor's constructor and should be preferred over AttachToComponent when a component is not registered."

That's all. Thanks in advance.



Solution 1:[1]

Remove

if (!RootComponent)
{
    RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("DuckBase"));
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mynkla