My Component moves when spawned into the editor!

Hello!

I have a c++ class for a ceiling light. It has a Static Mesh, and a point light, where the Static Mesh is the root component.

The problem is that when spawned into the editor, the light component is in the complete wrong location.

I can fix this by adding a set location in BeginPlay, but that makes it impossible to edit the scene because it’s lit incorrectly :frowning:

Even wierder, when i edit my blueprint child of the light, the blueprint viewport shows the light in the correct location, yet when i spawn it in, the component is in the wrong location!

Here’s my class, what can i do?
HEADER:
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

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

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

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

	UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = Mesh)
	UStaticMeshComponent* LampMesh;

	UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = Lighting)
	UPointLightComponent* Light1;
	
	/*UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = Lighting)
	UPointLightComponent* Light2;*/
};

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

#include "RSGame.h"
#include "FacilityLightCPP.h"


// Sets default values
AFacilityLightCPP::AFacilityLightCPP()
{
 	// 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;
	
	LampMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("LampMesh"));

	Light1 = CreateDefaultSubobject<UPointLightComponent>(TEXT("Light1"));
	Light1->SetRelativeLocation(FVector(0, 0, 0));
	//Light2 = CreateDefaultSubobject<UPointLightComponent>(TEXT("Light2"));

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

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

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

}

Hey TheSporech,

Can you try this:

 // Sets default values
 AFacilityLightCPP::AFacilityLightCPP()
 {
      // 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;
     
     LampMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("LampMesh"));
     SetRootComponent( LampMesh );
 
     Light1 = CreateDefaultSubobject<UPointLightComponent>(TEXT("Light1"));
     Light1->SetupAttachment(  );
     Light1->SetRelativeLocation(FVector(0, 0, 0));
 }

Same problem, sorry :S

Can you take a screenshot to show me what you mean?

Thanks.

Sure, so here’s how it looks in the blueprint editor viewport:

And when i add it to my scene, the light and mesh are really disjointed, the mesh is on the ceiling as it should (seeing as its root):

Yet the light component appears underneath the floor somewhere:

Can you make another version of your Lamp_BP from the C++ class and see if that one still has the same issue?

Sometimes when you have code changes, the children Blueprint don’t pickup the changes.

This is what I have, which is working well:

[.h]

#pragma once

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

UCLASS()
class AH497307_API ALamp : public AActor
{
	GENERATED_BODY()
	
public:	
	ALamp();
    
    UPROPERTY( BlueprintReadOnly, VisibleAnywhere, Category = "Lamp" )
    UStaticMeshComponent *Mesh;
    
    UPROPERTY( BlueprintReadOnly, VisibleAnywhere, Category = "Lamp" )
    UPointLightComponent *Light;
	
};

[.cpp]

#include "AH497307.h"
#include "Lamp.h"

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

    Mesh = CreateDefaultSubobject<UStaticMeshComponent>( TEXT("MESH") );
    SetRootComponent( Mesh );
    
    Light = CreateDefaultSubobject<UPointLightComponent>( TEXT("LIGHT") );
    Light->SetupAttachment(  );
}

TY, this worked. I just needed to recreate it