How to make an object collide with just one object?


In my game I have an object that can be destroyed by shooting at it, but if it hits a player, it damages the player. The problem I have is that sometimes the object gets so close to the player that when I shoot it, it gets destroyed (adds points) as well as deal damage to a player.

Is there a way to prevent this from happening within the OnCollisionEnter method? In other words, is there a way to script such that OnCollisionEnter method is called one-by-one rather than at once (at least that’s what I think is happening)?

Easiest way would be to select the gameobjects you want and at the top, add and set layers for each of them, like ‘Player’ and ‘YourObject’.

Then go to Edit > Project Settings > Physics and there will be a nifty grid with check boxes. You would uncheck where these two meet.

It wouldn’t take any additional code on your part, just setting the layers and then unchecking their intersection in the physics grid. This is good because then you don’t have to worry about anything odd happening with those collisions.