6
\$\begingroup\$

I've upgraded my project to LWRP, I've changed my materials to: LWRP/lit. But in one of the gamemodes i need to change one my materials' color by script. I used to do it simply like wallMat.color = Color.red, but at LWRP that code doesn't work, how to fix it?

Edit: LWRP = Light Weight Render Pipeline

\$\endgroup\$
3
  • \$\begingroup\$ Have you tried Material Property Blocks for this purpose? \$\endgroup\$
    – DMGregory
    May 21, 2019 at 17:15
  • \$\begingroup\$ Isn't that needs custom shader? Custom shaders not displays materials if you have LWRP. \$\endgroup\$ May 22, 2019 at 15:09
  • \$\begingroup\$ No, it doesn't require a custom shader, as shown below - it works with the default Lit shader that the LWRP uses. If you'd tried using MaterialPropertyBlocks, you would have been able to discover that for yourself even faster. Remember, we don't have access to some secret version of Unity that you don't - anything we suggest, you can try right now without waiting for an answer. \$\endgroup\$
    – DMGregory
    May 22, 2019 at 17:06

3 Answers 3

5
\$\begingroup\$

You can do this with a MaterialPropertyBlock, just like you would in the old pipeline to reduce the overhead of instantiating modified copies of materials. The only difference is that the shader property is called "_BaseColor" not "_Color":

// You can re-use this block between calls rather than constructing a new one each time.
var block = new MaterialPropertyBlock();

// You can look up the property by ID instead of the string to be more efficient.
block.SetColor("_BaseColor", Color.red);

// You can cache a reference to the renderer to avoid searching for it.
GetComponent<Renderer>().SetPropertyBlock(block);
\$\endgroup\$
8
  • \$\begingroup\$ It works very well on the gameobjects but can you tell me how can i change my characters material with that? It uses skinned mesh renderer on his child game object so i wrote smth like this: player.transform.GetChild(0).gameObject.GetComponent<SkinnedMeshRenderer>().SetPropertyBlock(block); but it didn't work. \$\endgroup\$ May 27, 2019 at 14:30
  • \$\begingroup\$ Does your character have multiple materials on it? If so, you may need to pass a material index as the second argument to target the desired material. It's also possible that your character shader's colour property is named something else. \$\endgroup\$
    – DMGregory
    May 27, 2019 at 14:50
  • \$\begingroup\$ It's LWRP/lit and i only have one material. \$\endgroup\$ May 27, 2019 at 14:55
  • \$\begingroup\$ Then it sounds like you should make a minimal complete verifiable example and post it as a new question. \$\endgroup\$
    – DMGregory
    May 27, 2019 at 15:20
  • \$\begingroup\$ I can't post my own made character that i am going to publish in my game? \$\endgroup\$ May 27, 2019 at 15:41
7
\$\begingroup\$

This works fine as well:

material.SetColor("_BaseColor", color);

\$\endgroup\$
0
\$\begingroup\$

I've tried matName.CopyPropertiesFromMaterial(otherMatName); and it worked! Creating another materials isn't the best way but in my case i've already had those materials and this way was the solution for me.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .