Unity collisions allow us to capture the moment and position of any two game objects colliding, the classic example is when a bullet or other projectile collides with a target. Let’s take the case where the gameobject we want to track is indeed named “bullet”. Bullets or other projectiles are almost always instantiated, or created, every time the player clicks their left mouse button. This causes the actual name of each bullet to actually show up as bullet(Clone). So this is the name we will want to track. Now let’s imagine we have a Unity Cube named Target, and we want to know when Target has been hit by bullet(Clone). Let’s also say that if Target is hit by a bullet, we want to destroy Target after one second. We could create the following script at attach it to the target (note in Figure 1 that CollisionDestroy.js has been added to the Target (in this case the Target is named Obelisk):
unity collision destroy example

Unity Collision – Here we have added the Collision Destroy script to the Target.

CollisionDestroy.js Whenever you see Destroy (gameObject, 1);, the number is the number of seconds that will pass before the gameobject is destroyed. Now sometimes we want the name of the object hit, so we can feed it to some other script down the road or create some more complex game mechanic than a mere destroy. In this case, we can modify the code like so: Debug.Log (col.gameObject.name); will give us the name of the object the bullet collided with (ie, the Target). In a soon upcoming tutorial I will discuss how we can send information between different scripts to create more nuanced and interesting game mechanics.