Nood Needs little assistance

Good day,

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.

void OnTriggerEnter(Collider other)
{
    if (other.gameObject.tag == "GreenOrb")
    {
        Destroy(other.gameObject);
    }
}

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.

1 Like

The color will determine the value . So I think I will change the tag to orb and when the time come I will get the info from the material. Thank you!

Wouldn’t this end up creating a brand new material, thus breaking batching and optimizations?
Probably your suggestion to use a component is better.

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! :slight_smile:

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.

1 Like

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.

1 Like

Just did a little test. It’s a little odd but you’re right it creates a new material, but only once.

So to OP:- For maximum efficiency use meshRenderer.sharedMaterial if want to get the material properties of an orb.