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 ?
$$anonymous$$y first guess would be that the mesh uses more materials than one and you'd need to loop through the .materials array to change them all.
The division between uncolored and colored areas looks weird though. $$anonymous$$ake certain there's no 2 buildings on top of eachother z-fighting
Answer by TheBusyBiscuit · Aug 13, 2016 at 04:33 PM
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[i].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 https://unity3d.com/get-unity/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.
@Nose$$anonymous$$ills @mrCookieSlime
Thank you very much for your answers, I was able to fix it with your help!
The Prefabbuilding had multiple children that represented different areas of that building.
I wrote this code to get all of them:
public Component[] components;
components = placeableBuilding.gameObject.GetComponentsInChildren<$$anonymous$$eshRenderer>();
for (int i = 0; i < components.Length; i++)
{
// e.g. color red
components[i].gameObject.GetComponent<$$anonymous$$eshRenderer>().material.color = Color.red;
}
Thank you very much!
Yes you have to be careful not to accidentally duplicate materials by using material
or materials[]
, but if you simply use shared$$anonymous$$aterials[i].SetColor("_Color", color);
then all your buildings using that material will go green when you just want to highlight one.
If you want to $$anonymous$$imize draw calls, use shared$$anonymous$$aterials and pre-instantiate the colored versions of all needed materials and assign them to the shared$$anonymous$$aterials of the highlighted building on the fly.