Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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
0
Question by goldmoogle · Aug 26, 2018 at 10:01 PM · material colormissingcomponentexception

Can't find material? (MissingComponentException)

I'm trying to make an effect where the color of the player and the color of the object they collide with swap during runtime.

Here's my code with the comments:

 public Color newColor;
 public Color oldColor;
 public GameObject Object;
 //upon collision
 void OnCollisionEnter (Collision collision) {
     //object collided with is stored
     Object = collision.collider.gameObject;
     //color of object is stored
     newColor = Object.GetComponent<Material>().color;
     //current color of subject is stored
     oldColor = gameObject.GetComponent<Material>().color;
     //subject becomes color of object
     GetComponent<Material>().color = newColor;
     //and vice versa
     Object.GetComponent<Material>().color = oldColor;
 }

My goal is to have the colors of the materials of those objects change, as this would theoretically update the color of all objects with these Materials applied, right?

The problem is, when I try to run it, I get a MissingComponentException error, in which it tells me the objects do not have a material attached to them (they do) and as such Unity cannot swap the colors of the Materials if it can't find them.

I've looked at people who have had this same error with other components but none of them seem similar to this issue. Any suggestions?

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
1

Answer by Bunny83 · Aug 26, 2018 at 10:42 PM

A Material is not a component and as such can not be attached to a gameobject. Therefore it can't be aquired using GetComponent. A material is used by almost every Renderer component, Usually a MeshRenderer or SkinnedMeshRenderer. The MeshRenderer component does have a material and materials property where you can access the material(s) used by this renderer.


Materials are usually stored as assets in your project and dragged into the materials array of a renderer. Note that the Renderer also has a sharedMaterial and sharedMaterials property. When using that you access the actual Material asset which may be shared by several objects. Changing this material will affect all objects that use the same material (and don't have their own instance). When you access material or materials Unity will automatically instantiate the shared material for this renderer. So changed to material will only affect this renderer.

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 kolalex01 · Mar 31, 2021 at 09:24 AM 0
Share

Thank you, I had the same problem, you solved it. :)

avatar image
0

Answer by yummy81 · Aug 27, 2018 at 04:48 PM

I modified your code a bit. Try this:

     public Color newColor;
     public Color oldColor;
     public GameObject Object;
     
     void OnCollisionEnter (Collision collision) {
         
         Object = collision.collider.gameObject;
         
         newColor = Object.GetComponent<MeshRenderer>().material.color;
         
         oldColor = gameObject.GetComponent<MeshRenderer>().material.color;
         
         GetComponent<MeshRenderer>().material.color = newColor;
         
         Object.GetComponent<MeshRenderer>().material.color = oldColor;
     }
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

91 People are following this question.

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

Related Questions

I get the error "MissingComponentException: There is no 'AudioSource' attached .." 3 Answers

how do you change the color of a prefab? 1 Answer

Can't edit materials 2 Answers

Problem with changing Material color using Animator States 0 Answers

Problem with updating material color at runtime. 0 Answers


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