Attach actor to scene root component not working. Help appreciated!

I have a problem where the actors do not properly attach to the parent actor’s SceneRootComponent. Any help would be appreciated!

I have an cpp actor named Terrain_Main_V3_1. I then have a blueprint actor that inherits from the cpp actor. In the blueprint I run the following function. The Target Component that I send in is the SceneRootComponent. However, their must be something wrong as the newly spawned actors do not attach to the SceneRootComponent. I wish to have them attached similar to the image:
UnrealEditor_I6svh4xKRg

void ATerrain_Main_V3_1::SpawnHexagons(TArray<FVector> VertexPosGrouped, USceneComponent* TargetComponent)
{
		// Ensure VertexPos is a multiple of 7
		if (VertexPosGrouped.Num() % 7 == 0)
		{
			// Calculate number of hexagons to spawn
			int32 NumHexagons = VertexPosGrouped.Num() / 7;

			// Iterate through each group of 7 vertices
			for (int32 HexIndex = 0; HexIndex < NumHexagons; ++HexIndex)
			{
				int32 StartIndex = HexIndex * 7;

				// Extract vertices for current hexagon
				TArray<FVector> HexagonVertices;
				for (int32 VertIndex = 0; VertIndex < 7; ++VertIndex)
				{
					HexagonVertices.Add(VertexPosGrouped[StartIndex + VertIndex]);
				}
				//const TArray<FVector> HexagonVerticesConst = HexagonVertices;


				//Spawn hexagon
				AHexagonData* NewHexagon = GetWorld()->SpawnActor<AHexagonData>(AHexagonData::StaticClass(), GetActorTransform(),FActorSpawnParameters());
				UE_LOG(LogTemp, Warning, TEXT("Spawning Actor"));
				if (NewHexagon)
				{
					// Assuming NewHexagon is a subclass of AActor and you want to attach it to the actor that spawned it
					NewHexagon->AttachToComponent(TargetComponent, FAttachmentTransformRules::SnapToTargetIncludingScale);
					NewHexagon->Initialize(HexagonVertices);
					UE_LOG(LogTemp, Warning, TEXT("Attaching Actor"));
				}
				else
				{
					UE_LOG(LogTemp, Warning, TEXT("Error Encountered"));
				}
			}
		}
}

So additional information, with the EU_LOG I confirmed that the code does pass (“Attaching Actor”)
Here is also an example of how I pass the SceneRootComponent. Note: The function name is different, because the function where the attachToComponent is, is rooted in this function. I use a custom event callable in editor to start the process.
image_2023-12-13_180120316
Here is also a figure of the component order:
image_2023-12-13_180332019
And here is a picture of how it currently looks like unattached:
image_2023-12-13_180505823

I have also tried using:

this
this->RootComponent
this->GetRootComponent()

I have also verified that each of them exist and are not NULL using a if statement

So I fixed the issue. The whole reason that it didn’t work was because a C++ actor doesn’t have a default scene root component unlike blueprints. Hence adding a sceneRoot to the actor fixed the issue.

UCLASS()
class V00001_WARHAMMER40K_API AHexagonData : public AActor
{
	GENERATED_BODY()

private:
	UPROPERTY(VisibleAnywhere)
	class USceneComponent* DefaultSceneRoot;
}
AHexagonData::AHexagonData()
{
        DefaultSceneRoot = CreateDefaultSubobject<USceneComponent>(TEXT("Default Scene Root"));
        RootComponent = DefaultSceneRoot;
}

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