Change Material of mesh without affecting all actors of class

How do I change the material of a static mesh of an actor without changing the material of all of the actors of that class?

I have over 1000 of these actors and when I try to change the material of the static mesh, it changes all of them. This is how I was trying to do it:

It’s changing all of them because you’re using get all actors of class. To change just one, you need to get a reference to the actor somehow, and run the set material function on it.

To better explain, it would help to know what condition or event should be causing the material to change. Like, should the stars change color when you shoot them with a spaceship or whatevs? Or is there a trigger box that causes it when the player pawn overlaps it?

1 Like

Exactly what @illspiritx said… You’re looping all stars and changing their material based on star type.

You’re not isolating a “Specific” single star instance to modify.

It seems the StarType variable was not being pulled over correctly, it was always 0. Doesn’t the array element of the for each loop reference specific actors of the class?

This was being done in the BeginPlay of the level. When I moved it to the BeginPlay of the actor BP, it seems to work and individual stars get their proper colors. I don’t know why I didn’t put it there to begin with. That still leaves the question about array element in the for each loop and why StarType was always 0. If For Each and array element doesn’t reach the specific star actors, how would you do it?

The get star type var is probably just pulling from the first actor you assigned as the var in the level blueprint. To get each star’s individual type, you’d probably need to cast to it from each element in the loop (not a good idea).

That said, if the star actor has a type var, your best bet would be to change the material in the star’s construction script instead of begin play. Not only would this avoid changing thousands of mats at runtime every time, it would also let you see the proper colors in the editor.

The return of Get all actors of class is of class type so no need to cast. The cast node will literally tell you this via a blue Note.

For loop Array Element is a reference to a specific instance. The values of the variables and components pulled from the instance are what they are at the exact moment they are referenced.

Level BP (level script) executes way before all of the placed actors in the level are loaded.

Calling Get all Actors of Class on its Begin play event will return all references from level data. Whether they are spawned or not isn’t considered here. Actors that are not spawned yet will return defaults… float (0.00), int (0), string (null), Name (none) etc.

If you add a 2-3 second delay you should get the expected results from applied variable settings.