Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 JustAnotherRobot · May 28, 2021 at 12:47 PM · prefabcolormaterialsrenderer.material.color

Renderer.material.color not changing color of prefab

Hello everyone,

my use case if very simple: I want the color of an object to change when it is clicked. If the user clicks somewhere else, it gets deselected and the color switches back to the original one. This already works with a cube for me by the simple code of:

 activeObject.GetComponent<Renderer>().material.color = Color.red;

alt text

Now I was importing an .obj file to make a prefab out of the mesh. If I click on the imported .obj it consists of a gameobject, a mesh and also a Material. I dragged the import into the scene, added some other components e.g. a meshrenderer and made it a prefab. The prefab also has the script with the line above for color changing. Selecting and deselecting the object works, however the mesh remains white-ish instead of turning red when active.

In the inspector, I assigned a defaul material (instance) to the materials/Element0 of the meshRenderer component component. When I now click on my object, the materialin the inspector also turns red as seen on the little colored sphere displayed. However in game the object still is white. alt text What am I missing here? Thank you in advance!

material-2.jpg (65.0 kB)
material-1.jpg (60.9 kB)
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by JustAnotherRobot · May 28, 2021 at 04:27 PM

I found out what the problem was and it probably is a real beginner's mistake. By dragging the imported .obj into the scene I got an empty parent gameobject named like the model with the actual model attached as a child. From my code I was just changing the parent's material. I adapted my code to first get the child component and then set it's material and everything works as expected now.

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
avatar image
0

Answer by EDDDDDDD · May 28, 2021 at 01:06 PM

Hi, what about changing the material of the object entirely. so activeObject.GetComponent().material. = . I suggest doing this as you won't have any issues with the default material not having a colour that you can edit.

There will be different ways of achieving and solving this issue. This is just the simplest way I can explain a solution to this.

Cheers

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 JustAnotherRobot · May 28, 2021 at 01:22 PM 0
Share

Thank you for your response! I tried out the line

 activeObject.GetComponent().material =Color.red;

However this gives me an error that the type argument cannot be inferred from the usage. If I add an after GetComponent I still receive an error. I also tried setting the Line to a Material which I defined over a public member variable. Still doesn't work. Adding the line activeObject.GetComponent<Renderer>().material = Matchanges the renderer's material to the new material I assigned but the gameobject itself still is colored white.

avatar image
0

Answer by Harry_Drew · May 28, 2021 at 02:10 PM

Hi, i think i realise the problem: activeObject.GetComponent(Renderer).material = Mat should be changed to activeObject.GetComponent(MeshRenderer).material = Mat. Make sure that the material that you assign in the inspector has the colour red.

Please let me know if this causes any issues and I will see what I can do. Cheers

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

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

Does Material.color, ParticleSystem.startcolor property clones/copies the material? 2 Answers

GUI button texture different color when script is on Instantiated object 0 Answers

Assign different materials to a range of same prefab? 1 Answer

How to change material of the active object? 1 Answer

My foreach or the Resources.LoadAll Is not getting the information I need 2 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