ROLL-A-BALL TUTORIAL #5


Adding the function of deleting the item

We add function of deleting the item in the game. The function is "Items disappear when the player touches items placed on the stage". It has the following function.
1. Create many items
2. Detect when the player touches item
3. Delete touching item

1. Create an object

At first, it creates an object that is a source of the item.
1. Click the “Create” of the “Hierarchy” view.
2. Click “3D Object → Capsule”.
3. Change a name into “Item” in the created Capsule.


Change position and scale for the “Item”.

  • Position(x: 0, y: 1, z: 0)
  • Scale(x: 0.5, y: 0.5, z: 0.5)

2. Create an object

We write out the object construction information as “Prefab” so that we can produce this created “Item” easily. Let's create a folder for “Item” and store prefab there.
1. Click the “Create” in the “Project” view.
2. Edit folder name to “Prefab”.


We write out the “Item” as “Prefab”. It is successful if the "Item" object changes the blue icon.
1. Select the “Item” in the “Hierarchy” view.
2. Drag and drop the “Item” object to the “Prefab” folder of the “Project” view.

3. Place the "Item" object

We place the created item into the "Scene".
1. Select “Item” in the "Prefab" folder of the "Project" view.
2. Drag and drop it onto the "Scene" view.

The object that was set up was placed in the "Scene" view. We will place items in a circle.

4. Contact judgment between “Player” and “Item”

Add contact judgment into Item. This function isn't a standard feature. We need to add a new component.
1. Select the "Item" object of the "Hierarchy" view.
2. Select “Add Component → New Script” of the “Inspector” view.
3. Change a name into "Item" and Click a "Create and Add" button.

At first, check the usable contact judgment call-back from API of Monobehaviour. This time, we add "OnTriiggerEnter" of contact judgment API. "OnTriiggerEnter" judgment only triggers. We add call-back in “Item.cs”.

Item.cs
using UnityEngine;
using System.Collections;

public class Item : MonoBehaviour
{
    // The call-back of contact trigger
    void OnTriggerEnter (Collider hit)
    {
    }
}

5. Change Item's contact judgment into the trigger

Since the judgment was set to Trigger, contact judgment must also be set to Trigger. Set Collider setting of Item to Trigger.
1. Select the "Item" object of the "Hierarchy" view.
2. Check “IsTrigger” of “CapsuleCollider” Component of the “Inspector” view.

6. Contact judgment with “Player”

It has become possible to make contact judgment with Trigger. However, we want to do the judgment with "Player" only.
There are several ideas. This time, we'll make it "check tag". The augment of OnTriggerEnter, "hit" contains the contact target information. Check this tag that contacted anything, and modify the "Item" component as follows to do special processing if it is "Player".

Item.cs
using UnityEngine;
using System.Collections;

public class Item : MonoBehaviour
{
    // Call-back called upon trigger of contact
    void OnTriggerEnter (Collider hit)
    {
        // Is the contact target “Player” tag?
        if (hit.CompareTag ("Player")) {

            // Do some processing
        }
    }
}

7. Set tag on “Player”

Also, set the "Player" object. Since we wrote the process of ‘When in contact with an object that has "Player tag"’, we also need the "Player tag" for the "Player" object.
1. Select the "Player" object of the "Hierarchy" view.
2. Change tag of "Inspector" view to "Player".

8. Delete Item at contact judgment

Set to delete objects when touching. Use the Destroy method to delete it.
Destroy is a method to delete an object. The object specified by the method will be deleted and disappear from the game.

Item.cs
using UnityEngine;
using System.Collections;

public class Item : MonoBehaviour
{
    // Call-back called upon trigger of contact
    void OnTriggerEnter (Collider hit)
    {
        // Is the contact target “Player” tag?
        if (hit.CompareTag ("Player")) {

            // Destroy the GameObject with this component
            Destroy(gameObject);
        }
    }

This will destroy the “Item” object when contact “Player”.

9. Update “Prefab”

Currently, only the "Item" object that has been set up will delete. Then update the value of "Prefab" so that the object placed as "Prefab" has the same setup as well.
1. Select the "Item" object of the "Hierarchy" view.
2. Click the “Apply” button of the “Inspector” view.

10. Organize “Item”

Finally, we will organize the items. The process is the same as setting the Stage. However, the item isn't a static object. We don't check "Static".
1. Click the “Create” in the “Hierarchy” view.
2. Select “Create Empty”.
3. Change a name into “Items”.
4. Select all the created "Item" objects and drag and drop to the "Items" object.