- Home /
MissingComponentException: There is no 'Renderer' attached to the "Enemy_PF" game object
I'm trying to flash an enemy when hit.
The code is as follows:
public function Hit() { animation.Play("hit"); Debug.Log("hit"); renderer.material.color = Color.red; }
But when the program runs, the enemy is not turn to red at all, and console displays following message:
MissingComponentException: There is no 'Renderer' attached to the "Enemy_PF" game object, but a script is trying to access it. You probably need to add a Renderer to the game object "Enemy_PF". Or your script needs to check if the component is attached before using it.
The gameobject is an instantiated-in-editor prefab, and the prefab contains a 3ds max imported mesh. So the gameobject should have a renderer.
How can I avoid the error and flash it?
Answer by whydoidoit · Jul 22, 2012 at 12:22 PM
The renderer is presumably not of the top level object but a child.
GetComponentInChildren(Renderer).material.color = Color.red;
Thank you for reply. But it was not enough for my case. I had to loop over renderers that collected from the call GetComponentsInChildren. Please read my own answer for details.
Answer by zeodtr · Jul 22, 2012 at 05:51 PM
In my case, the answer is the following code:
private function applyColor(a_color: Color) { var renderers: Component[] = GetComponentsInChildren(Renderer); for (var renderer: Renderer in renderers) renderer.material.color = a_color; }
I built the enemy mesh with many objects in 3ds max(in this case, 52 including biped parts. the enemy is a humaniod robot, and I had to built body parts with separated objects), and Unity seems to retain individual objects when importing. And Unity seems to assign one renderer per object. So I must set up all renderers. Now I am concerned of the performance...
Your answer

Follow this Question
Related Questions
Changing two different objects renderer colour 1 Answer
[CLOSED] renderer.enabled is set to true but does not show 0 Answers
Placing a GameObject on top of another GameObject. 1 Answer
Graphics.DrawTexture vs GUI.DrawTexture vs Graphics.Blit 0 Answers
What does the console mean when it says I need to attach a renderer? 1 Answer