playMaker

Author Topic: Change the Material of All Prefab Instances  (Read 3814 times)

bkups2003

  • Playmaker Newbie
  • *
  • Posts: 49
Change the Material of All Prefab Instances
« on: November 03, 2013, 05:54:53 AM »
Hello,

I'm trying to figure out if this is at all possible but not having any luck so far.

I have a Cube prefab with a blue material on it and i put 10 of them in a scene. Is it possible to change the blue material to another material on all 10 instances of this prefab? I'm trying to avoid iterating through each gameobject and setting its material seperately and hoping that there is a way to change it on all of them with a single action, since they all come from the same prefab. I attempted to use "set material" on the prefab itself and it did change successfully, however the change was not applied to anything in the game until after i stopped the game. At that point, the material on the prefab stayed the same as it was set in the game (red), and immediately applied to all the instances. I don't mind the material of the prefab being changed outside of the game being played as its an easy enough fix anyway. Problem is that it didn't apply to anything in the game until after the game was stopped.

Anyway... Anybody know of any solution to this?

Or if possible, is there any kind of "set material recursive" action? Something that could set the material of a gameobjects children? That would atleast make the iteration process easier, if there is no centralized way to change certain aspects of the iterations of a prefab.

Thanks alot

Breadman

  • Full Member
  • ***
  • Posts: 185
  • Derp
Re: Change the Material of All Prefab Instances
« Reply #1 on: November 03, 2013, 10:28:45 PM »
If I understand correctly, I think this might work:

Assign one shader to all the objects that you want to change color. In Unity, go to the shader's location in your project directory.

Create a state, or find the state that you're hoping will change the color of the shader when it's activated. Drag the shader from the project window directly into the state (as though you were dragging another action into the state from the action library).

A drop down window will appear, click "set property". A new action will automatically appear in the state, "targeting" the shader. From here, you can access the shader's properties (by clicking the "property" drop down menu), such as color. Doing so will affect all objects assigned with that material, since you're changing the materials itself and not the object.

Hopefully that does the trick!
« Last Edit: November 03, 2013, 10:31:04 PM by Breadman »