Hi, this is my enemy:
Enemy1 (gameobject with rigidbody,
collider, AI scripts etc)
→ Armature (child1)
→ the mesh (skinned mesh
renderer & material) (child2)
so the question is… how i can change the color of the enemy material when i hit him?
i have tried this… Make enemy Flash when Hit - Unity Answers
but obviously does not work because the material is in a child, so how i can do it? or how i should do it?
sorry for my english & thank you!
The short version:
Renderer renderers = GetComponentsInChildren< Renderer >()
foreach( Renderer renderer in renderers )
{
renderer.material.color = YOUR_COLOR
}
The slightly longer finer print:
- You might want to cache all the renderers at startup instead of fetching them on every action to save on performance.
- Make sure your material supports the color attribute.
- If you’re targetting anything other than PC, you might want to preset 2 materials and switch between them, as any attribute change to a material at runtime will create a new material instance.
Have fun.