# C++Change color all cubes first person blueprint

**URL:** <https://forums.unrealengine.com/t/c-change-color-all-cubes-first-person-blueprint/479345>\
**Category:** Rendering\
**Tags:** unreal-engine, color, cube, question\
**Created:** [February 24, 2021, 2:12pm UTC](https://forums.unrealengine.com/t/c-change-color-all-cubes-first-person-blueprint/479345 "2021-02-24T14:12:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_6a30aed8](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_6a30aed8](https://forums.unrealengine.com/u/anonymous_user_6a30aed8)\
**Post date:** [February 24, 2021, 2:12pm UTC](https://forums.unrealengine.com/t/c-change-color-all-cubes-first-person-blueprint/479345/1 "2021-02-24T14:12:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kehel18](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Kehel18](https://forums.unrealengine.com/u/Kehel18)\
**Post date:** [February 24, 2021, 3:31pm UTC](https://forums.unrealengine.com/t/c-change-color-all-cubes-first-person-blueprint/479345/2 "2021-02-24T15:31:31Z")

</div>

Hello! You can simply do smth like material on picture… This is done to avoid calling Name-based method inside Tick method

 ![330764-1.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/6/0/760cb7fae97e8002c7a950f5dd7589c6a5963191.jpeg)
