2
\$\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\$
  • \$\begingroup\$ Have you tried Material Property Blocks for this purpose? \$\endgroup\$ – DMGregory May 21 at 17:15
  • \$\begingroup\$ Isn't that needs custom shader? Custom shaders not displays materials if you have LWRP. \$\endgroup\$ – AmonRa's Channel May 22 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 at 17:06
0
\$\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\$
  • \$\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\$ – AmonRa's Channel May 27 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 at 14:50
  • \$\begingroup\$ It's LWRP/lit and i only have one material. \$\endgroup\$ – AmonRa's Channel May 27 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 at 15:20
  • \$\begingroup\$ I can't post my own made character that i am going to publish in my game? \$\endgroup\$ – AmonRa's Channel May 27 at 15:41
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\$
0
\$\begingroup\$

This works fine as well:

material.SetColor("_BaseColor", color);

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.