Change materials to all the elements from an array

I’m trying to get some help about this problem:
I want to apply different materials to different mesh actors according to which button I click on a screen widget. Finally and thanks to many tutorials I achieved my goal, except for a little inconvenience, in order to change the material I must click many times the same button so the material change takes effect one by one on each of the mesh actors.
I think this is because I plugged (widget blueprint) a “random integer in range node” into the get node which in turn is located between the “array variable reference” and the “static mesh target”, so I suppose that this “random integer node” gives a random index number per click, and this number decides to which of the “make array node” (in level blueprint) elements is going to change the material. Could somebody please help me to make the material changes to all elements from the “make array node”?, please, I’m really new to this.

Thanks in advance

Hello Chanito,

Have you tried using a ForEach Loop? This would allow you to perform the material change for each element in the array. Have a look at the documentation for the ForEach node and see if it’s what you’re looking for: A new, community-hosted Unreal Engine Wiki - Announcements - Unreal Engine Forums12-_For_Each_Loop

Thanks for the quick answer.

Yes, I have tried the “for each loop” option before with no good results. Since I’m new to game development may be is because I don`t know how to use the node properly in order to get what I want, but even so I tried an approach like this (attached picture), and the material only changes for the first item in the list of the “make array node”

I`m not using the “get all actors of class” node since the material change is intended to affect to different actors of different classes, because of that I used an array.

The gate and the initial foreach loop are unnecessary. Try adding a foreach loop after each on clicked event, like I have done in the picture below. I was able to get this to work without issue. Let me know how it works out.

Is working, thanks so much!

My last doubt is, when compiling the blueprint there are warnings, I think that is because of the “cast to static mesh” node that used, since no “cast to colorchange” seems to be available, so my questions are:

  • Which kind of “cast to” node should I use in order to avoid these warnings?, there are many of them, I used “cast to static mesh” in a sort of a 50/50 shot
  • List item
  • How do I discern between the use of any of the different nodes?, I mean, even when I checked the documentation couldn’t understand at all the use of this node

Thanks again, is working!

Glad to hear that you’ve got it working now.

  • ColorChange was the name of my actor blueprint (i.e. the actors that I was changing the materials of), so you’ll need to cast it to whatever your actor blueprint was called.

  • Assuming you’re asking about the use of the casting node, it is just used to check if the object that you are attempting to cast is of the same type as the object that you are casting to. So in my situation, casting the array element to ColorChange was ensuring that that particular element was an instance of my ColorChange blueprint. You can find additional documentation on performing casts here: Casting Quick Start Guide | Unreal Engine Documentation

Ok sir, I almost got it, but I won’t ask about it since I think that experience will be the one that clarifies to me how to get to the same result as yours.

Thanks again