C++ Can't Set Material For a static Mesh

Hello there,

I have a static mesh that I want to change the material for, but it doesn’t seem to want to change. I have tried to created a new project and test to see if the function SetMaterial works and it does. I have also tried to debug the function in my other project and it seems to assign it correctly, but no result shown. I have found this mesh off of the unreal store and seems fine manually changing it. I was wondering if there is any setting for it not to be dynamically set.

Also I am using the character meshs of the “Infinity Blade Warriors”.

I look forward to your reply. :slight_smile:

Solution: I was using the wrong mesh to assign the material too. I am now using the proper mesh.

Could you add some code or Blueprint ?

Here is the code I use:

float AdemoGameCharacter::TakeDamage(float damage, FDamageEvent const& damageEvent, AController* eventInstigator, AActor* damageCauser)
	// Call the base class - this will tell us how much damage to apply  
	const float ActualDamage = Super::TakeDamage(damage, damageEvent, eventInstigator, damageCauser);

	if (!mInvincible && mHealth > 0.0f)
            // For debug use 
		FASTLOG("player took damage...");
		mHealth -= 1.0f;
		mInvincible = true;

		// Activate damage animation
		TArray<UStaticMeshComponent*> c;
		for (UStaticMeshComponent* staticMeshComponent : c)
                    // Doesn't do anything...
			staticMeshComponent->SetMaterial(0, playerTransMaterial);

		// Deactive invicibility after at least 5 seconds...
		GetWorld()->GetTimerManager().SetTimer(playerTimerHandle, this, &AdemoGameCharacter::DeactivateDamageInvincibility, 1.0f, false);

	return ActualDamage;

Try staticMeshComponent->MarkRenderStateDirty();
Right after changing material

I have tried that and doesn’t seem to do anything. I notice that the function does it as well. Also I have tried the dynamic one, and doesn’t seem to work either.

One of your comment says:
// Deactive invicibility after at least 5 seconds…
But it looks like your timer is set to 1s. Have you tried to comment this and see if the real problem is the SetMaterial ?

I have comment out that function and still nothing has changed. I know SetMaterial works on a primitive object (like a cube). But I can’t seem the change this mesh.

Hey -

I’m not sure how you’re giving a value to playerTransMaterial however something that should work is if you use ConstructorHelpers. Specifically ConstructorHelpers::FObjectFinder() allows you get a reference to an asset in the content browser. The general syntax for this is:

static ConstructorHelpers::FObjectFinder<ObjectType> ReferenceVariable (TEXT("Path/To/Asset/Reference"));

Where ObjectType is the type of asset you want to find (UStaticMesh, UBlueprint, etc). In this specific case you would use UMaterial. The path to the asset reference can be obtained by right clicking on the asset in the content browser and selecting Copy Reference.

After this line you can use ReferenceVariable to ensure the correct object was stored using ReferenceVariable.Succeeded() which returns a boolean. I find this is helpful as the condition of an if statement before setting ReferenceVariable to a permanent UMaterial variable using PermanentMat= ReferenceVariable.Object; As the name ConstructorHelpers implies, this only works inside of the class constructor but the PermanentMat can then be used throughout the rest of the class.


Hello ,

Sorry for the late reply, I have been busy. I am currently now using the constructor call to get the material. At first, I was using the blueprint to assign it . I thank you for that. :slight_smile:

Also on another note, I have found out why my material wasn’t setting. I was looking at the wrong mesh. I based this project off of the ThridPerson example. I noticed that the mesh was a USkeletalMeshComponent rather than UStaticMeshComponent. I have found the GetMesh() method and set the matieral of that mesh and is now working properly. :slight_smile:

I want to thank you for all you did to help me.