Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by zeodtr · Jul 22, 2012 at 12:17 PM · renderermissingcomponentexception

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
4

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;
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image zeodtr · Jul 22, 2012 at 02:12 PM 0
Share

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.

avatar image
1

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...

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Changing two different objects renderer colour 1 Answer

Calculate time per frame 5 Answers

gameobject.renderer.material.color get null reference 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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges