UInstancedStaticMeshComponent ::RemoveInstance Bug

Hi all,

I’m currently facing a strange behavior when I call RemoveInstance from C++.

Basically I need to remove only one instanced static mesh at time, I managed to remove the correct one but for some reason the last instance mesh disappears but It’s collisions are still there (I can walk on hit but It’s not visible), then the more instances I remove the more this behavior gets worse, for example some meshes appears again and ohers are visible but without collision. I can post images or videos if needed;

So here’s the code called by the Player Tick function, it’s a basic linetrace:

	FHitResult Hit;

	FVector StartTrace = FirstPersonCameraComponent->GetComponentLocation();
	FVector EndTrace = (FirstPersonCameraComponent->GetForwardVector() * Range) + StartTrace;

	FCollisionQueryParams CollisionParams;
	CollisionParams.AddIgnoredActor(this);

	GetWorld()->LineTraceSingleByChannel(Hit, StartTrace, EndTrace, ECollisionChannel::ECC_WorldDynamic, CollisionParams);
	
	ABlock* IsBlock = Cast<ABlock>(Hit.GetActor());
	if (IsBlock == NULL)
	{
		CurrentBlock = nullptr;
		return;
	}

	CurrentBlock = IsBlock;
	CurrentBlockIndex = Hit.Item;    // Maybe this cause all the problems?

Then when the player press the left mouse button this code is executed:

if (CurrentBlock != nullptr)
    CurrentBlock->BreakBlock(CurrentBlockIndex);

ABlock.cpp

#include "Block.h"


// Sets default values
ABlock::ABlock()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = false;

	ISM_Block = CreateDefaultSubobject<UInstancedStaticMeshComponent>(TEXT("Terrain"));
}

// Called when the game starts or when spawned
void ABlock::BeginPlay()
{
	Super::BeginPlay();
	
	/**
      Here I simply place the cubes on the map, they are all 100x100 
     */
	for (int i = 0; i < 5; i++)
	{
		for (int j = 0; j < 5; j++)
		{
			FTransform transform(FRotator(0.0f, 0.0f, 0.0f), FVector(i*100.0f, j*100.0f, 0.0f), FVector(1.0f, 1.0f, 1.0f));
			SM_Block->AddInstance(transform);
		}
	}

}

void ABlock::BreakBlock(int32& index)
{
	ISM_Block->RemoveInstance(index);
}

I tried to implement an array of integers that holds a reference of the instances spawned by following this image that i found on the Live Training of the Instanced Static Meshes by Epic and then removing elements from that array whenever i call RemoveInstance but unfortunately nothing changed, sometimes the editor even crashed (let me know if you want me to post the code with the array implemented).

I already posted a question here: [Link][2] and there’s the possibility that there is a bug in the RemoveInstance function.

This is another test made by user Nachtmahr:

[Link][3]

[Link][4]

In the last example every box has no Collision regardless if mesh is visible or not.

UPDATE

I installed the 4.16.3 and then created a new C++ project using the FPS Template, I used the same exact code from the example above and it works without any problem, I can now assume that is a bug present only in the 4.17 version.

I have the same issue :frowning:

Same here, UInstancedStaticMeshComponent is broken in 4.17.1. I’ll make a minimum project with repro steps and will post it separately, without Hyne_'s custom code, so it’s easier for UE devs to track it.

edit: here is it: [Bug] Instanced Static Mesh Component is broken in 4.17.1 - Asset Creation - Unreal Engine Forums

Thank you for making that post, Flashback. It seems that this issue is already set to be resolved in 4.18, by looking at the post that you created. As such, I’ll be marking this issue as resolved. If anyone has any questions, please refer to the other post as it would be best to keep all information in one place.