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
1
Question by Questjon · Mar 23, 2014 at 08:22 PM · materialmaterialsrenderercompare

How to compare 2 materials?

Similar questions have been asked but none of those solutions seems to work for me.

I want to compare 2 materials to see if they are the same: i.e.

 if(material1 == object.renderer.sharedMaterials[0])
 {
  Debug.Log("Great success!");
 }

But this compares false even when I know it to be true. Outputting the name of the shared material shows that it is an instance of the material. How can I get the original in order to compare them?

Comment
Add comment · Show 2
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 robertbu · Mar 23, 2014 at 08:38 PM 0
Share

Not sure if this is your problem, but if you make any changes to your material...color, texture, offset, scale...Unity generates a new material instance. So making this comparison will fail if you are comparing a new instance to the shared material.

avatar image Questjon · Mar 23, 2014 at 08:42 PM 0
Share

Is there any way to compare the instantiated material to the original?

2 Replies

· Add your reply
  • Sort: 
avatar image
6

Answer by marjan · Jun 06, 2016 at 12:19 PM

You could compare by name:

Given you have a material in your project called "MyMaterial" it might be on your actual gameObject a "MyMaterial (Instance)". Because you assigned other textures, colors or so.

Now do something like this: String baseMaterialName = myBaseMaterial.name; String assignedMaterialName = myRenderer.sharedMaterial.name;

if (assignedMaterialName.Contains(baseMaterialName) ) { // here is your Match }

Comment
Add comment · Show 2 · 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 Side · Jun 06, 2016 at 12:30 PM 0
Share

Can material names be repeated? If so, and if you're looking for a specific instance, maybe it'd be better to use GetInstanceID() ins$$anonymous$$d.

avatar image AhsanNaeem · Aug 22, 2018 at 05:47 PM 0
Share

Thanks. It resolved my problem :)

avatar image
1

Answer by andrei-pit · Apr 21, 2020 at 06:33 AM

Comparing sharedMaterial works for me.

Example:

Debug.Log(GameObject.Find("obj1").GetComponent().sharedMaterial == GameObject.Find("obj2").GetComponent().sharedMaterial)

=> returns "False" even if the material names are the same

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 demirel04 · Feb 23 at 01:59 AM 0
Share

Is there a way to compare physical materials that you have them as a list and the ones attached to objects without using string compare.

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

24 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

Related Questions

Changing two different objects renderer colour 1 Answer

Issue with Mesh Renderer and Skinned Mesh Renderer 0 Answers

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

Having a problem with the second material (on the same renderer) 0 Answers

How do I change the material of a prefab when it is not in the hierarchy? 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