Download

Component moves when actor added to level

Heyo!

This bug is the bane of my life…

This has been happening for lots of my actors, but this is the clearest example.
So, I have a class for a Light object, pretty basic:
h:



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

#pragma once

#include "GameFramework/Actor.h"
#include "FacilityLight.generated.h"

UCLASS()
class HSHOOTER_API AFacilityLight : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	AFacilityLight();

	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
	
	// Called every frame
	virtual void Tick( float DeltaSeconds ) override;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Mesh)
	class UStaticMeshComponent* Mesh;
	
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Gameplay)
	UPointLightComponent* Light;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Animation)
	int FluctuationRate;

	void SwitchLight(bool b);
};



cpp:



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

#include "HShooter.h"
#include "FacilityLight.h"


// Sets default values
AFacilityLight::AFacilityLight()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;
	Mesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MeshComp"));
	Light = CreateDefaultSubobject<UPointLightComponent>(TEXT("LightComp"));

	RootComponent = Mesh;
}

// Called when the game starts or when spawned
void AFacilityLight::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame
void AFacilityLight::Tick( float DeltaTime )
{
	Super::Tick( DeltaTime );
	Light->SetIntensity(Light->Intensity + (FluctuationRate*sin(rand() % 360)));

}

void AFacilityLight::SwitchLight(bool b)
{
	Light->SetVisibility(b);
}


And I have a blueprint child of this class.
In the blueprint editor, I have made sure the point light component is where it should be in the lampshade:
Capture1.PNG.jpg

However, when I drag it into the level, the component moves!
Capture2.PNG.jpg

The only fix I know, is to set location in BeginPlay() which is horrible, and messy.

Anybody know how to fix this?

hi! try it:


Light->SetupAttachment(Mesh);

Doesn’t work :confused:

Put in here?


// Sets default values
AFacilityLight::AFacilityLight()
{
	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.PrimaryActorTick.bCanEverTick = true;
	Mesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MeshComp"));
	RootComponent = Mesh;
	Light = CreateDefaultSubobject<UPointLightComponent>(TEXT("LightComp"));
	Light->SetupAttachment(Mesh);
}

Hmm, works when I copy/pasted your constructor. Thanks! :slight_smile: