Strange behavior when removing a single instanced static mesh

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;

Actually It’s really difficult to explain what happens ahaha.

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

I hope I was clear enough, and sorry for any misspelling.

Thanks in advance :slight_smile:

Hmm did a quicktest and it looks like its a Bug. I also noticed it removes 2 visually and pop back in sometimes! Usually the the last index always disapears (left far side in the gif). Feel free to Post it as full Bug report you can use your example and my dummy script, something seriously messed up with removal:

Important to point out that every red box has no Collision regardless if mesh is visible or not.

Hey, thanks for trying it out, I just posted it in the Bug Report sections and I’ve included your examples that have been very useful.

I created a new project using the 4.16.3 version of the engine and now it works without any problem, I can now assume that there is a bug in the RemoveInstance function only present in the 4.17.1 version.

I already submitted a Bug Report, if anyone is interested here’s the link: UInstancedStaticMeshComponent ::RemoveInstance Bug - Blueprint - Epic Developer Community Forums