FiveArcher76571

Unity 101 - CoinCollector.cs

Sep 16th, 2023
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.90 KB | Source Code | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class CoinCollector : MonoBehaviour
  6. {
  7.     // A private variable storing the number of coins we've collected
  8.     int coinsCollected = 0;
  9.  
  10.     // This function is called whenever a game object overlaps a trigger
  11.     void OnTriggerEnter2D(Collider2D collision)
  12.     {
  13.         // Get the game object we overlapped with
  14.         GameObject triggedObject = collision.gameObject;
  15.  
  16.         // If the game object has the tag "Coin"
  17.         if(triggedObject.CompareTag("Coin"))
  18.         {
  19. // Debug.Log() logs a specific message to the console, helping you keep track of what is going on.
  20. Debug.Log("Coin Collected");
  21.  
  22.             // Increment coins collected
  23.             coinsCollected = coinsCollected + 1;
  24.  
  25.             // Destory the game object we overlapped with
  26.             Destroy(triggedObject);
  27.         }
  28.     }
  29. }
  30.  
Advertisement
Add Comment
Please, Sign In to add comment