Changing the Materials using UMG Buttons

Hi,

I am new to game development and Unreal Engine 4. I am using version 4.6.1

I am trying to change the materials using UMG for some days but still I couldnt find proper tutorials or blueprints.

So I just created a Widget with Buttons like shown in the image. (I created 5 buttons namely Color1, Color2, Color3, Color4 and Color5). If I OnClick any button, the material should change accordingly.

I have also shown Level Blueprint to show the Widget.

Can somebody please help me to find the solution for this issue.

Any blueprint or tutorial might help.

Thanks in advance!

The Shape_Cone is a StaticMeshActor, I would create a StaticMeshActor variable in the UMG widget, and pass the Shape_Cone as a reference.

Then you can bind each button’s OnClick event to change the material on that Shape_Cone reference to the appropriate Material using the Set Material node.

Hope it helps :slight_smile:

Hi, Can you check whether I have done is right or wrong?

Widget Blueprint
I have created StaticMeshActor variable and named it as Shape_cone. I passed that as an to Actor blueprint and called an event “Update Material”.

Actor Blueprint
When “Update Material” event is called, change the staticmeshactor (i.e Cone_Shape) to the respective material.

But still I didnt get output. Just for clarification. Thanks!

When I bind for a button, the buttton is not working during runtime.

You have shown binding for Text Block?

Fake Widget Button:

“Bind” in the bottom right> This is how I would set a variable in BP from Bind:

So you could set a texture in the Mesh via this method.

You’re on the right track!

I’ve created a simple widget to demonstrate the concept I was mentioning, this widget has 2 simple buttons, I labeled them M1 and M2. M1 will set the material banana bark to the cone, M2 will set banana leaf to the cone. As well as a variable that holds my cone reference.

When I add the widget to the viewport, I pass it my cone reference.

In my widget I will add the onClicked event for my buttons, each button will be responsible for another material.

29556-example.gif

As you can see, when I click the cone, it brings up the widget, then clicking the buttons will change the material accordingly!

Hope it helps! :slight_smile:

Hi,

Thank you so much. It works!

Hi,

How would I do the same thing for more than 3 Actors? For example

Case1: With 3 Cones

Case2: 1 Cone, 1 Cylinder and 1 pipe

I have to do the same thing for more than 7 objects. So if I copy the same blueprint for the rest, it would be more messy. I know very well that I need to do it using forloop or using switch statement. But I am not sure how? Can someone please help me to solve this issue?

Thanks in advance!

If you don’t know how to use a blueprint for loop then I suggest you take a quick look at the flow control documentation.

Use it with the Get all actors of class method.

https://docs.unrealengine.com/latest/INT/BlueprintAPI/Utilities/GetAllActorsOfClass/index.html

Hi,

I am trying to do the same thing using Actor blueprint (not level blueprint) but I am not getting the material changed. I dont know where I am wrong.

The problem is … you are not setting the cone to anything. You’re grabbing the cone of the widget and setting it to itself. When you do “Set Cone”, the cone has to be a reference to the .

Okay. I understood the problem now. But one more problem is that, the StaticMesh1 (Shape_Cone) is the StaticMeshComponent in which I set its category as an Actor. When I tried to give as an reference to Cone, its not accepting. Hence its displaying “The type of StaticMesh1 is not compatible with Cone”.

Since I think Cone is a “StaticMeshActor”, I tried by creating a new variable type “StaticMeshActor” and given as an reference to Cone, but its not working.

I don’t really understand where you got confused.

It’s pretty straight forward, you need to pass it the static mesh actor as a reference when you’re making the widget.

I made a blueprint out of a static mesh actor and used the shape_cone as the static mesh. In the blueprint I am using the event for the OnClick in order to add my widget to the screen.

As you can see, this is pretty much identical to the previous answer I gave you for the level script, because it’s the same script, the only thing that would need modification is … what you’re passing to the widget as your static mesh actor.

I made a slight adjustment to the widget function so that it gets removed whenever I choose a material (that way if I have multiple cones I don’t keep popping widgets on top of widgets).

I can drag out as many of my cone blueprints as I want.

Each one is responsible for his own material change.

As you can see, the answer I provided originally is the same answer I’m giving you now, the only thing I changed was the reference that goes into “Cone”. Which is what I suggested you should do.

I’m sorry if my explanation was not clear enough.

No worries :slight_smile: I can mostly understand your way that you explain.

Can I know what is that self in step4.png? How you have passed that to cone (Static Mesh Actor) Reference?

Create a blueprint, when it asks what class, choose Static Mesh Actor, the blueprint is now a child of Static Mesh Actor.

So you can pass it “self” because it is a Static Mesh Actor.

Thank you so much once again!

I am getting the error message like below: Can I know how to avoid this error? because I am doing the same process in 3 levels. After changing the materials in one level and when I tried to change the level, the editor crashes.

31433-error+message.png

seems like a completely different problem. I suggest you start a new topic for it.

I have posted a new topic about this and got an answer. The problem is that this solution works in Unreal editor 4.7 but not in 4.6. But anyways thanks.