SetStaticMesh at runtime does not work

Hi,

I have a Weapon-Class, which contains a StaticMesh reference.

UPROPERTY(EditAnywhere)
UStaticMesh* ProjectileMesh;

When this weapon is firing, it creates and spawns a Projectile-Class and calls a function called Initialize, which gets among other things the ProjectileMesh. This function then sets the mesh as the static mesh for the projectile.

void AFleroProjectileBase::Initialize(UStaticMesh* ProjectileMesh)
{
	ProjectileMeshComponent->SetStaticMesh(ProjectileMesh);;
}

However, this does not work. Ingame, the projectile is not visible.
When I set the static mesh in the constructor of the projectile, however, it works.

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

	ProjectileMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("VisualRepresentation"));
	ProjectileMeshComponent->SetupAttachment();
	ProjectileMeshComponent->SetMobility(EComponentMobility::Movable);

// Everything above is always called
// Everything below is the way how it works right now. But I want to set the static mesh in the Initialize method instead
	static ConstructorHelpers::FObjectFinder<UStaticMesh> SphereVisualAsset(TEXT("/Game/Geometry/Meshes/1M_Cube_Chamfer.1M_Cube_Chamfer"));
	if (SphereVisualAsset.Succeeded())
	{
		ProjectileMeshComponent->SetStaticMesh(SphereVisualAsset.Object);
		ProjectileMeshComponent->SetRelativeLocation(FVector(0.0f, 0.0f, -100.0f));
		ProjectileMeshComponent->SetWorldScale3D(FVector(0.8f));
	}

	 = ProjectileMeshComponent;
}

As you can see, I already set its mobility to Movable, which seemed to be the solution in similar questions, but does not work for me.
When I run the project and look at the Projectile while runtime, the correct static mesh is assigned (see picture below, where I chose a UFO mesh), but as I said, you cannot see it.

Do you have any idea, where I might have missed something?

How are you calling:

void AFleroProjectileBase::Initialize(UStaticMesh* ProjectileMesh)
 {
     ProjectileMeshComponent->SetStaticMesh(ProjectileMesh);;
 }

And, are you sure that both the ProjectileMeshComponent and the ProjectileMesh are both valid references?

Hi,

the Initialize-function is called as follows:

void UFleroShipWeapon::FireWeapon()
{
	AFleroProjectile* Projectile = GetWorld()->SpawnActor<AFleroProjectile>(
		AFleroProjectile::StaticClass(),
		GetSocketLocation("Weapon"),
		FRotator::ZeroRotator,
		ProjectileSpawnParams
		);
	Projectile->Initialize(ProjectileMesh);
}

The ‘ProjectileMesh’ is set through a derived blueprint within the editor. Therefore I am pretty sure the reference is correct. Additionally, as I mentioned before, when I look in the ‘World Outliner’ of Unreal while playing, the spawned projectile does in fact has the correct mesh (see screenshot of my original post). It is just not visible.
Further, the reference for ProjectileMeshComponent should be correct, as it is an attribute of the class, which I do not alter after creating it in the constructor.

Greetings and thanks for helping me!

In the code in the original post you call SetupAttachment( ) on ProjectileMeshComponent before calling = ProjectileMeshComponent.

If you are still doing this, I would suggest making ProjectileMeshComponent the root component and not calling SetupAttachment( ).

If that doesn’t work, I would make a SceneComponent as the and then call ProjectileMeshComponent->SetupAttachment().

Thanks, it worked!

I took a deeper look into it and figured something out.
When calling ProjectileMeshComponent->SetRelativeLocation() in the constructor, it actually does set the relative location, but when called in the Initialize() function, the value was set for its absolute position.

Now that I use your suggestion with a SceneComponent as a root, calling ProjectileMeshComponent->SetRelativeLocation() in the Initialize() function actually does set the relative position.

So the problem from the start was simply a misplacement of the object rather than a rendering issue.

I am not quite sure why this behaves like this, but it works now.

If you create an answer to this question with the thing you just said, I would gladly accept it. However, if you have the time, I would be interesed in an explanation, why calling ProjectileMeshComponent->SetRelativeLocation() behaves differently when called in the constructor than in a seperate method, when the ProjectileMeshComponent is the root.

Greetings
Chriz

Actually, it finally works on runtime since you did this line, if it’s not Movable, Set Static Mesh will not work anyway
image