material.color colors the GameObject only partially

Hello,

I would like to change the color of the buildings (instantiate by clicking buttons), dependent if their current position is valid for placement or not (green= valid, red = invalid, white = placed)

I’m using following C# code:

placeableBuilding.gameObject.GetComponentInChildren<MeshRenderer>().enabled = true;
placeableBuilding.gameObject.GetComponentInChildren<MeshRenderer>().material.color = Color.red;

Same code for green and white.

But the building gets colored only partially. It is kinda weird for me because some areas with the same texture (roof) get colored, other areas not.

Also independent of the color, always the same area of the example building gets colored.

Can someone please give me a hint explain me what I’m doing wrong ?

76009-green2.png

76010-red22.png

You shouldn’t really access the materials by using renderer.material
Because it may actually be the case that your Renderer has more than 1 Material.

Use renderer.sharedMaterials instead. And then loop through that array.

Now Material.color follows the same example, depending on your Shader, you can have multiple color variables.
Look what Shaders the Materials of your Mesh use and then set their Colors by using

renderer.sharedMaterials.SetColor(“_Color”, color);
If your shader only uses one single color, using material.color would of course be fine as well.
But if it does use multiple colors and you want to set all of them then just replace _Color with their Variable Names, you can find their Variable Names by opening the .shader file of your Shader.
If you are using one of Unity’s built-in shaders, _Color should work just fine, if not then download Unity’s built-in shader package from the Download Archive
Download Archive
and see yourself.
Also, it looks like you are instantiating a new building onto the same position as your last one…
Don’t do that, delete the existing building when instantiating a new one at that position or simply change the materials of your existing building instead of creating a new one.