Changing ACharacter material at runtime without crashing

I’m trying to write a function that checks every material in a skeletal mesh component to see if its slot name is “Skin,” and if it is, to change that material to some new, alternative skin material that I set elsewhere. My first attempt is as simple as I can possibly make it:



    void ABaseCharacter::SetSkin(UMaterialInterface* newSkin, USkeletalMeshComponent* newMesh) {
    	newMesh->SetMaterialByName("Skin", newSkin);
    }


Commenting this function’s body out eliminates the crash, so I’m fairly certain this is what’s causing it, but I’m lost as to why; I thought maybe it was crashing when I called this on a skeletal mesh component that didn’t have any materials labelled “Skin,” but I verified that only calling it on a mesh component that I know for a fact has a skin material still crashes. The traceback, if it’s useful, is as follows:

I don’t see any of my own classes referenced there, but given the context I’m convinced it has something to do with how I’m trying to change my material; is it just flat-out not possible to do this at runtime? I *have * gotten it to work in blueprint by using the SetMaterialByName node, but I’m trying to include this in the base Character class that all my other ASomeCharacter.cpp children inherit from, so it’s not really feasible.

try


void ABaseCharacter::SetSkin(UMaterialInterface* newSkin, USkeletalMeshComponent* newMesh)
{
	if (newSkin == nullptr)
	{
		//Add to log error
		return;
	}

	if (newMesh == nullptr)
	{
		//Add to log error
		return;
	}

	newMesh->SetMaterialByName("Skin", newSkin);
}

to be sure the objects are valid

Hmm, it looks like they’re definitely valid; no logging occurs before the crash, and adding a sanity check doesn’t prevent it:



if (newMesh && newSkin){
		newMesh->SetMaterialByName("Skin", newSkin);		
}


I also did a test in blueprint, and feeding it the same values I used here does result in the desired behavior, so I’m fairly confident that the correct values are getting plugged in.