- Home /
Change enemy color when hit
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... http://answers.unity3d.com/questions/16548/make-enemy-flash-when-hit.html
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!
Answer by MartinCA · Sep 06, 2013 at 07:58 PM
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.
Your answer
Follow this Question
Related Questions
Material doesn't have a color property '_Color' 4 Answers
Changing two different objects renderer colour 1 Answer
Change material color at runtime!! 5 Answers
Changing a materials color in C# 2 Answers
using rgb to change color c# 1 Answer