← Каталог
Unity C# — скрипты для новичков — Монетка и счёт (OnTriggerEnter)
Фрагмент из «Unity C# — скрипты для новичков»: Монетка и счёт (OnTriggerEnter).
using UnityEngine;
public class CoinPickup : MonoBehaviour
{
[SerializeField] private int value = 1;
void OnTriggerEnter(Collider other)
{
if (!other.CompareTag("Player")) return;
ScoreManager.Instance?.AddScore(value);
Destroy(gameObject);
}
} using UnityEngine;
public class CoinPickup : MonoBehaviour
{
[SerializeField] private int value = 1;
void OnTriggerEnter(Collider other)
{
if (!other.CompareTag("Player")) return;
ScoreManager.Instance?.AddScore(value);
Destroy(gameObject);
}
} using UnityEngine;
public class ScoreManager : MonoBehaviour
{
public static ScoreManager Instance { get; private set; }
public int Score { get; private set; }
void Awake()
{
if (Instance != null && Instance != this)
{
Destroy(gameObject);
return;
}
Instance = this;
}
public void AddScore(int amount)
{
Score += amount;
Debug.Log($"Счёт: {Score}");
}
} using UnityEngine;
public class ScoreManager : MonoBehaviour
{
public static ScoreManager Instance { get; private set; }
public int Score { get; private set; }
void Awake()
{
if (Instance != null && Instance != this)
{
Destroy(gameObject);
return;
}
Instance = this;
}
public void AddScore(int amount)
{
Score += amount;
Debug.Log($"Счёт: {Score}");
}
}