UStaticMeshComponent::SetStaticMesh() not working

UStaticMesh * mesh = Cast(StaticLoadObject(UStaticMesh::StaticClass(), NULL, *m_accessoryUrls[type]));
staticMeshActor->GetStaticMeshComponent()->SetStaticMesh(mesh);

I tried the new setmesh function in 4.6(above) and it didn’t seem to work , the mesh is not set. So in the end I set it directly as I did in 4.5(below) and it works fine.Just reporting this , could someone check it. m_accessoryUrls is simply a vector of reference URLs.

UStaticMesh * mesh	 = Cast<UStaticMesh>(StaticLoadObject(UStaticMesh::StaticClass(), NULL, *m_accessoryUrls[type]));	
staticMeshActor->GetStaticMeshComponent()->StaticMesh = mesh;

Hi TensaiSumo,

Have you tried using the line

staticMeshActor->SetStaticMesh(mesh);

instead of

staticMeshActor->GetStaticMeshComponent()->SetStaticMesh(mesh);

I did a real basic project set up with a custom actor class containing a static mesh component, and a function that set the static mesh for that component. I referenced a static mesh that was in the project and set it using the first line above and it worked fine. If that doesn’t work for you, could you provide some additional information about how you are setting up your class, in particular staticMeshActor?

Hi ,

I am using the AStaticMeshActor class and it doesn’t seem to have a SetStaticMesh function. But in the end I got it working. It seems SetStacticMesh() function can be used only on Meshes with Mobility set to movable. Since the mesh I created was static by default the function seemed to fail. Here is the code I am using now.

//actor spawn params
FActorSpawnParameters params;
params.bNoCollisionFail = true;

// creating the static mesh actor  
AStaticMeshActor* staticMeshActor					  = static_cast<AStaticMeshActor*>(m_world->SpawnActor(AStaticMeshActor::StaticClass(),NULL,NULL, params));	
//getting the static mesh and assigning it to the static mesh component
UStaticMesh * mesh									  = Cast<UStaticMesh>(StaticLoadObject(UStaticMesh::StaticClass(), NULL, *m_accessoryUrls[type]));	
//change mobility of actor before assigning mesh
staticMeshActor->SetMobility(EComponentMobility::Movable);
staticMeshActor->GetStaticMeshComponent()->SetStaticMesh(mesh);

I am glad to hear that you were able to get it to work. I had not had an opportunity to look into the SetStaticMesh function prior to the release of 4.6, so I wasn’t aware of the restriction on mobility.