Hi;
I have started a new project using first person template, What I want to do is to use the following to change all cube colors in the scene:
// Fill out your copyright notice in the Description page of Project Settings.
#include "ColoredCube.h"
#include "Materials/MaterialInstanceDynamic.h"
#include "Components/StaticMeshComponent.h"
#include "Materials/MaterialInterface.h"
// Sets default values
AColoredCube::AColoredCube()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
}
// Called when the game starts or when spawned
void AColoredCube::BeginPlay()
{
Super::BeginPlay();
auto Cube = FindComponentByClass<UStaticMeshComponent>();
auto Material = Cube->GetMaterial(0);
DynamicMaterial = UMaterialInstanceDynamic::Create(Material, NULL);
Cube->SetMaterial(0, DynamicMaterial);
}
// Called every frame
void AColoredCube::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
float blend = 0.5f + FMath::Cos(GetWorld()->TimeSeconds)/2;
//float blend = 0.7f;
DynamicMaterial->SetScalarParameterValue(TEXT("Blend"), blend);
}
as you can see I am using auto Cube = FindComponentByClass(); to get the Cube that I have added to a local blueprint that I created earlier, but I wasnt to fine the rest of the cubes that template automatically imported and change their color too. The cubes have type staticmeshactor. Blend is a scalarparameter that I add to control the color in material editor
//Thank you