changing static mesh material instance c++

I have a few UPROPERYs setup for UMaterialInstance* variables. I’m randomly picking one and wanting to assign that material instance to an actor’s static mesh.

I am able to get the static mesh but for some reason the material isn’t changing.

Below is in the header file:

    // Possible balloon colors ----

	UPROPERTY(EditAnywhere)

	UMaterialInstance* MaterialRed = nullptr;



	UPROPERTY(EditAnywhere)

	UMaterialInstance* MaterialGreen = nullptr;



	UPROPERTY(EditAnywhere)

	UMaterialInstance* MaterialYellow = nullptr;



	UPROPERTY(EditAnywhere)

	UMaterialInstance* MaterialPurple = nullptr;

Below is in the cpp file:

   // Randomize balloon color

	float MaterialRnd = FMath::RandRange(0, 3);



	UMaterialInstance* RandomMaterial = nullptr;

	FString Color = "Purple";



	if(!MaterialRed || !MaterialYellow || !MaterialGreen || !MaterialPurple){

		UE_LOG(LogTemp, Error, TEXT("Balloon material references not valid."))

	}

	else{

		UE_LOG(LogTemp, Error, TEXT("Balloon material rnd color var = %s."), *(FString::SanitizeFloat(MaterialRnd)))

	}



	if(MaterialRnd == 0){

		RandomMaterial = MaterialRed;

		Color = "Red";

	}

	else if(MaterialRnd == 1){

		RandomMaterial = MaterialYellow;

		Color = "Yellow";

	}

	else if(MaterialRnd == 2){

		RandomMaterial = MaterialGreen;

		Color = "Green";

	}

	else{

		RandomMaterial = MaterialPurple;

	}


       UStaticMesh* BalloonMesh = GetBalloonStaticMesh();  
    
	   if(BalloonMesh){    


			BalloonMesh->SetMaterial(0, RandomMaterial);

			BalloonMaterial = RandomMaterial;

			UE_LOG(LogTemp, Warning, TEXT("Balloon material set to: %s"), *(Color))



		}

it would be much easier to create it in this way, creating a list of materials:

UPROPERTY(EditAnywhere, Category = "List Materials")
TArray<class UMaterialInterface*> ListMaterial;

results:

275711-1.png

add the amount of materials you will use

275712-2.png

this would look at the size of the list a random material

 if (ListMaterial.Num() > 0)
 {
	 "YourStaticMesh"->SetMaterial(0, ListMaterial[FMath::RandRange(0, ListMaterial.Num() - 1)]);
 }

This is a cool new thing you’ve taught me - but it doesn’t answer why my material isn’t changing. You’re using the same function “SetMaterial()” as I am and that function is what is not working. I need to know what is wrong with the code.

I figured it out.

Firstly, I was getting the StaticMesh when I should have been getting the StaticMeshComponent. Secondly, I needed to set the mobility (No idea why I had to do this, I suppose it’s just an unreal thing).

// Returns balloon static mesh
UStaticMeshComponent* const ABalloon::GetBalloonStaticMesh(){

	return Cast<UStaticMeshComponent>(GetComponentByClass(UStaticMeshComponent::StaticClass()));

}

and

        if(RandomMaterial && Self){
		
		UStaticMeshComponent* BalloonMesh = GetBalloonStaticMesh();

		if(BalloonMesh){

			BalloonMesh->SetMobility(EComponentMobility::Movable);
			BalloonMesh->SetMaterial(0, RandomMaterial);
			BalloonMaterial = RandomMaterial;
			UE_LOG(LogTemp, Warning, TEXT("%s material set to: %s"), *(BalloonMesh->GetName()), *(Color))

		}

	}
	else{
		UE_LOG(LogTemp, Error, TEXT("Balloon material not set."))
	}

I understand, I hope it works for future projects.