Hey all, I am new to Unreal engine, and I come from the Unity world but trying to just get my feet wet here. Something simple I figured would be to, on key press, change the material base color on a sphere from white to red. This turns out to be far more complicated than I expected.
I created a new C++ class that inherits from AActor called “TriggeredActor”. In my Settings, I setup an Input called “Trigger” and set it to the K key. Finally on the Input component of the Actor I set Input Auto Receive Input to Player 0. The on screen message from the Trig() method displays with no issue the correct name of the material, but the last line for SetVectorParameterValue causes everything to just close. As there are no issues when I comment out that line.
void ATriggeredActor::BeginPlay()
{
Super::BeginPlay();
InputComponent->BindAction("Trigger", IE_Pressed, this, &ATriggeredActor::Trig);
}
void ATriggeredActor::Trig()
{
UStaticMeshComponent* sphere = this->FindComponentByClass<UStaticMeshComponent>();
sphere->SetMobility(EComponentMobility::Movable);
UMaterialInstanceDynamic* material = (UMaterialInstanceDynamic*) sphere->GetMaterial(0);
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, material->GetName());
material->SetVectorParameterValue(FName(TEXT("BaseColor")), FLinearColor(0.9f, 0.1f, 0.1f));
}
This is a general combination of things that I’ve found from around the internet, this isn’t a popular topic I guess.
I know this isn’t Unity so can’t compare but this would be something like:
void Update()
{
if(Input.GetKeyDown(KeyCode.K))
{
Renderer renderer = GetComponent<Renderer>();
renderer.material.SetColor("_Color", Color.red);
}
}
Thanks for any help you guys can give.