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.

![1.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/512084/b2a3ec54-0deb-b0fa-ec11-1478e9bd90ae.png)
Change position and scale for the “Item”.
![2.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/512084/5d4b29db-d611-a682-68ac-95f607f1a01b.png)

- 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”.

![3.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/512084/f6956220-16f2-3d9a-4958-04792a0e63ad.png)
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.
![4.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/512084/71b9a30a-aaab-d130-b498-655ada502c67.png)
![5.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/512084/65ac041c-0cfe-fa66-7176-9ddea9abfa90.png)

#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.
![6.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/512084/8f370052-c046-7762-695c-c83d682897bd.png)

#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.
![7.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/512084/45adde8f-fb59-23ea-1b8f-5c79bb9481a3.png)
#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.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/512084/3838801f-c85d-43af-4773-27715e5d9549.png)

#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.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/512084/958d02a6-608c-4680-141b-6b3aacd27679.png)
![10.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/512084/83cfea24-2a5b-4c31-bbd9-e4fa2fd924ae.png)

#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.
![11.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/512084/1b9a061c-fdc8-6b49-aa36-a92a21c76dcc.png)

#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.
![12.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/512084/3b95440f-2b61-40d1-9a99-5333db404e13.png)