Change Color of Children Based on Parent

I’m trying to set the color of a child object based on it’s parent. I’d like to set the color when the child is created, inside the child object.

Here’s what I have so far…

void Start ()
{
    Color parentColor = this.GetComponentInParent<Renderer>().material.color;
    GetComponent<Renderer>().material.color = parentColor;
}

I’m able to set the color of the child object by changing parentColor to something like Color.cyan – so I think the problems lies in that I’m not getting the color of the parent correctly. Any insight on a solution would be greatly appreciated.

Durgal

Just one child object per parent? If that’s true, I think you’d want :

Color parentColour = GetComponentsInParent<Renderer>()[1].material.color;

hopefully I didn’t mess up that syntax. GetComponentInParent returns for its own GameObject, as well as any parents (ie: in your code snippet, you’re probably catching the child’s color and then just re-applying it to itself, and because you’re using the singular version, it returns only the one).
If the child’s depth is greater, you could adjust the index, and if it’s a random depth, perhaps a script on the parent that stores its color, and then any children can search for the Component in their parents by that script’s name.
Hopefully that works & makes sense.