Custom spline mesh not showing up in game

Hello

I am using UE4.10.2 and i recently wanted to learn about spline mesh and how they work. I managed to make them work in blueprint, but i wanted to make splines generated by code in C++ to make tracks. I tried loading a mesh and assigning it to the spline in C++, and the mesh, the spline mesh and the spline itself are showing up in the editor mode. But when i press play the spline mesh is invisible. I tried assigning another mesh to the spline through the menus, and the splinemesh does appear in that case. I don’t know what i am doing wrong.

This is the constructor of my spline actor:

ASplineTest::ASplineTest()
{
 	// 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;
	RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("SceneComp"));
	PathSpline = CreateDefaultSubobject<USplineComponent>(TEXT("Path"));
	PathSpline->AttachParent = RootComponent;
	int32 f = 1;
	for (int32 i = 0; i < 10; i++)
	{ 
		PathSpline->AddSplinePoint(FVector(10*i*100, 10*f*50, i*30), ESplineCoordinateSpace::World);
		f = f*-1;
	}
	NewMeshComponent = NewObject<UStaticMeshComponent>(this, TEXT("Mesh"));
	ConstructorHelpers::FObjectFinder<UStaticMesh> MeshObj(TEXT("/Game/ThirdPerson/Meshes/roadplane.roadplane")); 
	NewMeshComponent->SetStaticMesh(MeshObj.Object);
	NewMeshComponent->SetWorldScale3D(FVector(10, 10, 1));
	TheMaterial = NewObject<UMaterial>(this, TEXT("Material"));
	static ConstructorHelpers::FObjectFinder<UMaterial> MatFinder(TEXT("Material'/Game/ThirdPerson/Meshes/RampMaterial.RampMaterial'"));
	if (MatFinder.Succeeded())
	{
		GEngine->AddOnScreenDebugMessage(1, 14, FColor::Red, "succeeded");
		TheMaterial = MatFinder.Object;
	}
	else
		GEngine->AddOnScreenDebugMessage(1, 14, FColor::Red, "not succeeded");

	TheMaterial->bUsedWithSplineMeshes = true;
	PathSpline->IsVisible = true;
	PathSpline->SetVisibility(true);
	
}

This is the OnConstruction function

void ASplineTest::OnConstruction(const FTransform & Transform)
{  
	for (int32 i = 0; i < PathSpline->GetNumSplinePoints() - 1; i++)
	{ 
		USplineMeshComponent* SplineMesh = ConstructObject<USplineMeshComponent>(USplineMeshComponent::StaticClass(), 
			this);

		SplineMesh->CreationMethod = EComponentCreationMethod::UserConstructionScript;
		SplineMesh->SetMobility(EComponentMobility::Movable);
		SplineMesh->SetVisibility(true);
		SplineMesh->AttachParent = PathSpline;

		 
		UMaterialInstanceDynamic* dynamicMat = UMaterialInstanceDynamic::Create(TheMaterial, NULL); 
		SplineMesh->SetMaterial(1, dynamicMat); 

		SplineMesh->SetStaticMesh(NewMeshComponent->StaticMesh);   

		FVector pointLocationStart, pointTangentStart, pointLocationEnd, pointTangentEnd;
		PathSpline->GetLocalLocationAndTangentAtSplinePoint(i, pointLocationStart, pointTangentStart);
		PathSpline->GetLocalLocationAndTangentAtSplinePoint(i + 1, pointLocationEnd, pointTangentEnd);
		SplineMesh->SetStartAndEnd(pointLocationStart, pointTangentStart, pointLocationEnd, pointTangentEnd);
	}
	
	RegisterAllComponents();

 


}

Here are illustrations to show the problem:

this is the spline mesh with the spline as created and loaded through C++

This is ingame, the spline mesh is not visible

In the editor it is visible

I change the splinemesh in the editor menu and it shows in the editor and ingame

Ok i finally fixed it, i had to use an array to store the spline meshes. Now it works

Hi Dan,
Thanks for sharing this . Could you please share the updated code also ? Thanks Alam.

How did you fix this? It’s not clear what’s meant by array to store the spline meshes. Thanks in advance!