Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class CoinCollector : MonoBehaviour
- {
- // A private variable storing the number of coins we've collected
- int coinsCollected = 0;
- // This function is called whenever a game object overlaps a trigger
- void OnTriggerEnter2D(Collider2D collision)
- {
- // Get the game object we overlapped with
- GameObject triggedObject = collision.gameObject;
- // If the game object has the tag "Coin"
- if(triggedObject.CompareTag("Coin"))
- {
- // Debug.Log() logs a specific message to the console, helping you keep track of what is going on.
- Debug.Log("Coin Collected");
- // Increment coins collected
- coinsCollected = coinsCollected + 1;
- // Destory the game object we overlapped with
- Destroy(triggedObject);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment