- Home /
How to assign a color to a game object & destroy it when it collides with another game object of same Color
So i have a gameobject of blue color, now how do i assign that color to a game object is through code or do we have a tool in Unity and after this is done. i want the game object to destroy upon colliding with another gameobject of same color is there any method that i can use or a function that i can call.
Hi, there are many ways you could do this and it depends on what way you will setup your objects.. but here's one approach that's pretty straightforward.
When you are creating your gameObject , assign it's colour and identity though code. For the identity I just mean an easier way to identity the gameObject than by it's colour value. For example you could assign a tag to it: Tags.
Then, when you object collides with another object compare the tags of the objects and if they match you can destroy the relevant gameObject or have whatever other outcome you feel like.
Hope that helps!
Are we talking about a 3D object's material or a sprite?
3D Objects, the above Image is a 3d Object with Box Collider attached to it.
Answer by finlay_morrison · Jul 27, 2017 at 12:13 PM
im no pro at unity but you could do something like this you could set a variable and go like this.spriterenderer.color and set the same for the other object, then oncollision do an if statement to compare the two variables, im no pro but i think you could do it this way.