Part 19 - Deleting Coins.

By Jamie Chatterton / 2018-06-12

At present, all the coins at just falling into infinity. It'd be nice if they were captured and deleted.

As a precursor to creating a game, let's add a little area below the floor object as a coin collection area.

I'm going to leave this as an exercise for the reader now. But essentially it's going to need to be a minimum of a block, the same width as the floor. Just below the floor and with no gaps.

You can add another material if you want.

Here's mine…

Open this image in a new tabAdded collection object
Added collection object

It's the selected dark object. I named it collection.

Git respository: https://bitbucket.org/hiveit/unity-tutorial-coin/src/014_AddedCollectionBasicObject/

We're going to listen to objects colliding with this and destroy them, essentially.

Add a Component script to the collection object. DeleteCollidingObject.

We are going to add a new Event function, OnCollisionEnter.

The default outline looks like

private void OnCollisionEnter(Collision other)

The parameter type of Collision which is the object that just collided with this object. It has a property of gameObject which is a reference to the object itself. So we can destroy it.

So this function, this class, in full looks like

public class DeleteCollidingObject : MonoBehaviour {
    private void OnCollisionEnter(Collision other) {
        Destroy(other.gameObject);
    }
}

And with that we end up with

Git respository: https://bitbucket.org/hiveit/unity-tutorial-coin/src/015_DeleteCoins/

More posts in this series.