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
1
Question by Anymeese · Jun 05, 2014 at 04:33 AM · c#materialbooleaninstance

[C#] Checking InstanceOf?

When running my Debug.Log tests, I get the following output. n is a GameObject variable, and landingHexMat is a Material variable.

 n.GetComponent<MeshRenderer>().material == marker_hex_green (Instance) (UnityEngine.Material)
 landingHexMat == marker_hex_green (UnityEngine.Material)

I want (n.GetComponent().material == landingHexMat) to return TRUE, but it returns false. What do I need to change???

Comment
Add comment · Show 4
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 · Jun 05, 2014 at 04:36 AM 0
Share

With the pseudo-code here and lack of context, it is difficult pinpoint your problem. Note that if you make any change to your materials, your original materials will be replaced by new material instances. This will invalidate any comparison to the original material.

avatar image Anymeese · Jun 05, 2014 at 07:19 AM 0
Share

how can I compare and see if the material on a gameobject is an instance of the material referenced by the variable? $$anonymous$$y problem is the "(Instance)" part

avatar image JohnnySunshine · Jun 05, 2014 at 09:40 AM 0
Share

Have you tried .shared$$anonymous$$aterial ins$$anonymous$$d of .material?

avatar image Anymeese · Jun 05, 2014 at 07:46 PM 0
Share

just tried this, but got the exact same results. said marker_hex_green (Instance) rather than just marker_hex_green. Same for if I use material.name or .toString() (and same with shared$$anonymous$$aterial)

Is there no instanceof or something?

EDIT: Turns out I'm supposed to use .equals ins$$anonymous$$d of ==, but the comparison still does not return true.. Right now the only way to get it to return true is if I compare the rgb (not alpha) color properties, but SURELY theres a better way than this...

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by rutter · Jun 05, 2014 at 08:02 PM

You could query the instance ID of the sharedMaterial property:

 bool match = originalMat.GetInstanceID() == renderer.sharedMaterial.GetInstanceID()

That assumes you're looking for a common instance.

You could also write an extension method which checks the shader and parameters used by each material. If the shaders and parameters match, it stands to reason that the materials are equivalent.

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 Anymeese · Jun 05, 2014 at 08:55 PM 0
Share

The instanceID's are not the same, just tested with debug.log. What exactly do you mean by checking the shaders and parameters? What parameters exactly?

avatar image rutter · Jun 06, 2014 at 12:00 AM 1
Share

If the instanceID's aren't the same, they're not the same object. The equality check is firm and unyielding.

It's important to distinguish between reference equality (these two references point to exactly the same memory location) and value equality (these two references point to objects which are functionally identical).

What I mean by checking the shaders and parameters: your material, like any object, has some data associated with it. This tends to include a shader (the program which runs the material) and some parameters (like color, textures, and so on).

If two materials use the same shaders with the same parameters, they're functionally equivalent. If they don't, they're not.

avatar image
1

Answer by Bunny83 · Jun 05, 2014 at 10:09 PM

Are you sure that they are actually the same? Keep in mind when ever you access ".material" you will create an instance / copy of the original material. To aviod this you always have to access sharedMaterial.

Just to make that clear, even if you access .material just one time, Unity will create a seperate instance for this renderer which will be used from now on.

Also keep in mind that if you change something on the sharedMaterial, every instance that uses the shared material will be affected as well.

Read the .material page carefully. It's explained there.

Comment
Add comment · Show 6 · 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 Anymeese · Jun 05, 2014 at 10:35 PM 0
Share

Did not know that, thanks! But yes I'm sure they're actually the same because I have another function that does:

 n.GetComponent<$$anonymous$$eshRenderer>().material = landingHex$$anonymous$$at;
avatar image Bunny83 · Jun 05, 2014 at 10:39 PM 0
Share

That won't work. Whenever you access (read or write) .material, Unity will create a copy of the material.

You have to use

    n.GetComponent<$$anonymous$$eshRenderer>().shared$$anonymous$$aterial = landingHex$$anonymous$$at;

When you want to check the material you have to use shared$$anonymous$$aterial as well:

     if(n.GetComponent<$$anonymous$$eshRenderer>().shared$$anonymous$$aterial == landingHex$$anonymous$$at)
     {
         //...
avatar image Anymeese · Jun 05, 2014 at 10:56 PM 0
Share

Just tried that and it still is not returning True..

Heres my full code for reference. I only have 4 materials that the hexes should be, so I should NEVER receive the error message, which I am, since the boolean checks dont return true.

http://i.imgur.com/kSdWqs0.jpg

avatar image Bunny83 · Jun 05, 2014 at 11:51 PM 0
Share

Are you sure you don't use .material anywhere in your code? $$anonymous$$aybe from another script? I did something similar in the past and it worked as expected. The only reasons why you might get an material instance are:

  • you accessed .material or .materials somewhere.

  • you modified a material property in the inspector of one of your tiles which might also create an instance.

avatar image Anymeese · Jun 06, 2014 at 02:24 AM 0
Share

just double checked, and I did not access .material anywhere else or in any other script. The only use of "material" was when declaring the $$anonymous$$aterial variables, and in the two functions i screenshot(ted?).. The game does make them flash by lowering and raising the alpha level when moused over, but wouldnt it still be considered the same material?

Show more comments

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 one material instance affects all instances? 4 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Check to see if any bool instance is true? 1 Answer

Toggling bools automatically using coroutines 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