TriggerBox C++ Problem - Mesh only visible in Editor but not in Game

Greetings everyone!
I tried to write a base TriggerBox class includes a StaticMeshComponent. Everything works the way I expected exept that the StaticMesh I include is only visible in the Editor but not when I pressed the play button. Could anyone tell me why this is? I inherit my class from ATriggerBox which is inherit from AActor. So I really don´t see why there is this error.

Here is my .h:

UCLASS()
class EMWD_API AMesh_TriggerBox : public ATriggerBox
{
	GENERATED_BODY()
	
    public:
        AMesh_TriggerBox(const FObjectInitializer& ObjectInitializer);

        UFUNCTION(BlueprintCallable, Category = "Debug")
            void Debug(FString Msg);

        UFUNCTION(BlueprintCallable, Category = "TriggerEvent")
            virtual void OnOverlap(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool FromSweep, const FHitResult& SweepResult);

        UFUNCTION(BlueprintCallable, Category = "TriggerEvent")
            virtual void OnEndOverlap(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);

        UPROPERTY(VisibleDefaultsOnly, BlueprintReadOnly, Category = "VisibleMesh")
            UStaticMeshComponent* Visible_Mesh;
   	
};

Here is my .cpp:

AMesh_TriggerBox::AMesh_TriggerBox(const FObjectInitializer& ObjectInitializer)
{

    RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
    Visible_Mesh = ObjectInitializer.CreateDefaultSubobject<UStaticMeshComponent>(this, TEXT("Visible_Mesh"));

    Visible_Mesh->AttachTo(RootComponent);

    ATriggerBox::GetCollisionComponent()->bGenerateOverlapEvents = true;
    ATriggerBox::GetCollisionComponent()->SetRelativeScale3D(FVector(1, 1, 2));

    ATriggerBox::GetCollisionComponent()->AttachParent = RootComponent;
    ATriggerBox::GetCollisionComponent()->OnComponentBeginOverlap.AddDynamic(this, &AMesh_TriggerBox::OnOverlap);
    ATriggerBox::GetCollisionComponent()->OnComponentEndOverlap.AddDynamic(this, &AMesh_TriggerBox::OnEndOverlap);
}

void AMesh_TriggerBox::OnOverlap(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool FromSweep, const FHitResult& SweepResult)
{

    Debug("EnterTriggerBox - Chair has been destroyed");
}

void AMesh_TriggerBox::OnEndOverlap(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{

    Debug("ExitTriggerBox");
}

void AMesh_TriggerBox::Debug(FString Msg)
{
    if (GEngine)
    {
        GEngine->AddOnScreenDebugMessage(-1, 1, FColor::Red, Msg);
    }
}

Hello ,

I have tried to reproduce your issue, but when I set the static mesh in the Editor I was able to see it both In-Editor and while playing the game. Is there any more information you could provide that could be helpful in attempting to reproduce your issue?

Have you tried to create a blueprint based on the class and set the static mesh through that, or is that not something you’re looking to do?

I look forward to hearing back, have a great day,

Sean Flint

Greetings STAFF,

Thanks a lot for your reply! I really didn´t done much more than this. I had just started a new blank project to make some tests because I want to code more in my projects. However, the TriggerBox code was the first one I wrote. I compiled the code and put an instance of it on my map to test it. Everything worked perfect. The event was triggered and I got the debug message but the mesh was invisible. I also checked everywhere in the editor if visible is true. So I have no idea what the problem is. But when you say everything worked when you tried to reproduce my error then it is maybe a bug. I will try to start a new project and re-do it. Maybe it then works for me as well.

By the way. I don´t want do create a blueprint based on the class, The plan was to inherit from this class the final special classes also in code. For example a Switch + Triggerbox combination and stuff like that.

But anyway, I will retry and hope that it work then. Thanks a lot for taking the time to test my code. It is good to know that there is nothing totally wrong with my programming skills.

All the best

I tried it again but it still don´t work. So I made a video of how I do it using the code I posted here. Maybe you could find the error I made? Or is this really a hard bug?

Hello ,

Thank you for creating the video. However, I am a having a bit of trouble seeing the issue. I understand that you’re not able to see the mesh, but that was because of your Mesh_Triggerbox Instance settings. Once you turned off the Actor Hidden in Game option, the actor appeared in your game. Was this not what you were trying to achieve? It seemed like once you unchecked that option, your mesh was appearing normally as you wanted it to.

Have a great day,

Sean Flint

Hallo,
The problem is when I set Actor Hidden in Game to false that not only the mesh is visible but also the TriggerBox itself. But the TriggerBox should be invisible and only the Mesh should be visible in game. But I could not find a setting constellation in which this was possible.

All the best

Hello ,

I see what the issue was now, I missed it the first time I watched the video. If you’d like, you could zip up your project and upload it to Dropbox or Google Drive and provide a link so that I could take a look and see exactly what is causing the issue, since I have been unable to reproduce it in projects that I have created.

Have a great day,

Sean Flint

Hello,
thanks a lot for that offer. Here is the link:

All the best

Hello ,

After spending some time looking at your project and looking into the code, I’ve figured out that you don’t want to be using the TriggerBox class as a base for your Mesh_TriggerBox class. That particular class is not really meant to be used in that way. Try creating your Mesh_TriggerBox class in a fresh project and basing it on the Actor class instead of the TriggerBox class. Then, add a collision component to it and use that as your trigger instead, and make sure you add your mesh component. This should allow you to get the result you’re looking for.

Have a great day,

Sean Flint

Thanks again for taking the time to work out my problem. With Actor it actually worked. But from a programmer perspective I can not really see why this is. A TriggerBox is inherited from Actor. So it should have his functionality if it was not overwritten and I could not find the point where this happend. However. It works with Actor and I will go with that. Thanks a lot for your help!

All the best