- Home /
Question by
Sawka · Jun 19, 2021 at 05:53 PM ·
materialschildchildrenmaterial colormaterial renderer
Set Specific Child's Material
Hi. I'm trying to make a system where if the player hits a specific object, its color changes between 3 predefined colors. However, I want the player to have skins, and I want to give the color only to certain meshes of the children objects.
Right now, what I have is:
private CharacterController controller;
public Material[] colors; //I have already set 3 colors in inspector.
public Renderer[] skins; // I have put all the skins I want to change here, and didn't put any of the skins I don't want to change color.
public int c;
void Start()
{
controller = GetComponent<CharacterController>();
skins = GetComponentsInChildren<Renderer>(); //I think the problem is here, since this gets all the components in children objects.
}
private void OnControllerColliderHit(ControllerColliderHit hit)
{
if (hit.transform.tag == "ChangeColor")
{
Destroy(hit.gameObject);
foreach (Renderer rend in skins)
{
{
c = Random.Range(0, colors.Length);
rend.material = colors[c];
}
Any ideas, please?
Comment
Answer by DenisIsDenis · Jun 20, 2021 at 06:07 AM
You can add a "Change Color" tag (or some other tag) to the child objects to be painted and check for this tag:
foreach (Renderer rend in skins)
if (rend.CompareTag("Change Color"))
{
// change color here
}