This is my simple code to just destroy an object with the tag “GreenOrb” on trigger enter.
Instead of writing this code double and making it unnecessarily long, I want to ad another tag “PurpleOrb”.
What will be the shortest way to do that?
This is also for me to learn for future.
You can tag your orbs as “Orb”. And if there’s some other place in your code that needs to know the color of an orb then it can check the orb’s material property.
You don’t always need to use a tag to determine how to treat an object. You can check for the presence of a component, the object’s name, or any other property of an object.
Unity shouldn’t create a new material when checking a material’s property.
I wondered where you got that idea from and did a Google search and the first result is this .
It’s a small world!
I’m not sure why your material ID kept changing when creating a new material instance. Perhaps you was assigning the new instance to your object somewhere?.
But either way, this isn’t really relevant as just reading a material’s properties shouldn’t create a new material.
And be wary when reading Unity’s blog posts. They’re very guilty of taking a huge dump on older versions of Unity when promoting their latest and greatest feature.
After refreshing my memory and running a test:
meshRenderer.material creates a new Material instance
meshRenderer.sharedMaterial doesn’t create a new Material
and meshRenderer.material will only create a new material when changing its properties. There would be no need to create a new material when reading a property.
meshRenderer.material creates a new material as soon as you access it, as was discussed in the post you linked. Just doing meshRenderer.material creates a new instance of the Material. This happens in Unity 2017.4. Not sure if anything changed in later versions.