Techshop

Unity Tutorial 19 - Deleting Coins

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…

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

GIT TAG 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 TAG 015_DeleteCoins

Unity - A simple tutorial < Unity Tutorial 18 - Keyboard Input - movement Unity Tutorial 20 - Walls >