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.
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?
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.
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.