SetCollisionEnabled / SetCollisionProfileName AFTER SpawnActor seems to have no effect.

Hi guys,
I’m really struggling with this issue. I need to enable/disable collision and manipulate collision channels at runtime. Just to make my test simple for now I just enable/disable collision without manipulating channels, so the code is just two lines.

My constructor creates the Default Sub Object and assign it
mesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT(“Mesh”));
RootComponent = mesh;

If I place this code in the constructor of my C++ item class it has effect, my character can walk through the object, and there is no collision.

mesh->SetCollisionProfileName(“NoCollision”);
mesh->SetCollisionEnabled(ECollisionEnabled::NoCollision);

If I place the same code in another function, after I called SpawnActor() to create my item, then the code has no effect.

UStaticMeshComponent* MyMeshComponent = ((UStaticMeshComponent*)obj->GetDefaultSubobjectByName(“Mesh”));
MyMeshComponent->SetCollisionProfileName(“NoCollision”);
MyMeshComponent->SetCollisionEnabled(ECollisionEnabled::NoCollision);

This is done server side and item is replicated.

Any idea how to set collision after SpawnActor?

Hi,
I found the problem. The issue is that collision in the constructor is replicated to the clients automatically, as the constructor is also run on clients, while the code I have after Spawn Actor is only executed on the server. The solution I adopted now is to have a variable called collisionType

UPROPERTY(ReplicatedUsing = OnCollisionChanged)
int collisionType;

which is replicated to all clients. When that variable changes the function OnCollisionChanged is executed on all clients,. In OnCollisionChanged() I placed the collision code I want.

Note that the function is not executed server side, so you need to call that server side yourself as well.

1 Like