Skip to content

Instantly share code, notes, and snippets.

@TheMehranKhan
Created October 27, 2023 20:38
Show Gist options
  • Save TheMehranKhan/ab9ec4bf89e89e48843f15c06cee2493 to your computer and use it in GitHub Desktop.
Save TheMehranKhan/ab9ec4bf89e89e48843f15c06cee2493 to your computer and use it in GitHub Desktop.
A collectible item that gives points to the player's score when the player collides with it.
// Author: themehrankhan
using UnityEngine;
/// <summary>
/// A collectible item that gives points to the player's score when the player collides with it.
/// </summary>
public class CollectibleItem : MonoBehaviour
{
/// <summary>
/// The number of points that the collectible item gives to the player.
/// </summary>
public int points = 10;
void OnTriggerEnter(Collider other)
{
/// <summary>
/// Checks if the collectible item collided with the player.
/// </summary>
/// <param name="other">The collider that collided with the collectible item.</param>
if (other.CompareTag("Player"))
{
// Get the player's ScoreManager component.
ScoreManager scoreManager = other.GetComponent<ScoreManager>();
// Add points to the player's score.
scoreManager.AddScore(points);
// Destroy the collectible item.
Destroy(gameObject);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment