Files
VR-GAME/Assets/Script/ScoreManager.cs

61 lines
2.0 KiB
C#

using UnityEngine;
using TMPro; // Dùng UnityEngine.UI nếu bạn xài Text thường
public class ScoreManager : MonoBehaviour
{
public TextMeshProUGUI scoreText; // Kéo UI Text điểm số vào đây
private int currentScore = 0;
void Start()
{
currentScore = 0;
UpdateScoreUI();
// CHẨN ĐOÁN LỖI:
Collider col = GetComponent<Collider>();
if (col == null)
Debug.LogError("<color=red>LỖI NẶNG: Object này (" + gameObject.name + ") CHƯA CÓ COLLIDER. Hãy add Box Collider ngay!</color>");
else if (!col.isTrigger)
Debug.LogWarning("<color=yellow>CẢNH BÁO: Collider của rổ chưa tích 'Is Trigger'. Hãy tích vào!</color>");
Debug.Log("<color=white>Script ScoreManager đang hoạt động trên: " + gameObject.name + "</color>");
}
// Bắt va chạm kiểu Trigger (Xuyên qua)
private void OnTriggerEnter(Collider other)
{
GhiDiem(other.gameObject, "TRIGGER");
}
// Bắt va chạm kiểu Vật lý (Đập vào nhau) - Dự phòng nếu bạn chưa tích Is Trigger
private void OnCollisionEnter(Collision collision)
{
GhiDiem(collision.gameObject, "PHYSICS");
}
void GhiDiem(GameObject obj, string type)
{
Debug.Log("PHÁT HIỆN VA CHẠM [" + type + "] với: " + obj.name + " | Tag: " + obj.tag);
if (obj.CompareTag("Ball") || obj.name.Contains("Sphere") || obj.name.Contains("ball"))
{
currentScore += 2;
UpdateScoreUI();
Debug.Log("<color=cyan>===> GHI ĐIỂM THÀNH CÔNG! Điểm hiện tại: " + currentScore + "</color>");
Destroy(obj, 0.2f);
}
}
void UpdateScoreUI()
{
if (scoreText != null)
{
scoreText.text = "Score: " + currentScore;
}
else
{
Debug.LogWarning("ScoreText chưa được kéo vào ScoreManager!");
}
}
}