I have a script that changes the character color randomly to one of those specified in color. How can I make the color change only for Game Objects in the "body" Code (CSharp): private Renderer rend ; public Color [] color; public GameObject [] body; private void Start() { { rend = GetComponent<Renderer>(); rend.material.color = color [Random.Range(0, color.Length)]; } }
You are currently modifying the color of the material. I assume, all your renderer use that material instance. If they are supposed to use different colors, you will want to use property blocks and set them per renderer. You could also use a different material for the renderers that should be recolored and those that should not. However, this could end up in having many materials and this will be bad for performance, because it increases the number of draw calls. If you don't know what that means, nevermind. Your clue is property blocks Something like this (pseudo code, as I am on mobile. Might not compile. Also _Color might not be the right property name) Code (CSharp): var propertyBlock = new MaterialPropertyBlock(); foreach(GameObject go in body) { foreach(Renderer r in body.GetComponentsInChildren<Renderer>(){ r.GetPropertyBlock(propertyBlock); propertyBlock.SetProperty("_Color", color); r.SetPropertyBlock(propertyBlock); } }