Crash on material reference?

I’m trying to swap out a static meshes material at runtime like so…

if (Icons[i] != NULL && Objects[i] != NULL)
		{
			if (ObjectMaterialsDyn[i] != NULL && Objects[i]->StaticMeshComponent->GetMaterial(0) != NULL)
			{
				if (bHighlighted)
				{
					if (Objects[i]->StaticMeshComponent->GetMaterial(0) != ObjectMaterialsTransDyn[i])
						Objects[i]->StaticMeshComponent->SetMaterial(0,ObjectMaterialsTransDyn[i]);
				}
				else
				{
					if (Objects[i]->StaticMeshComponent->GetMaterial(0) != ObjectMaterialsDyn[i])
 						Objects[i]->StaticMeshComponent->SetMaterial(0, ObjectMaterialsDyn[i]);
				}
			}
}

It seems to work fine yet every now and again the game will crash on either SetMaterial lines and I cant seem to figure out why any suggestions?

#UPROPERTY()

is this array

ObjectMaterialsDyn

UPROPERTY() ?

If not perhaps the materials / material instances are being garbage collected, leading to the crash :slight_smile:

Rama

Yep that seems to have sorted the issue!

Thank you Rama!