You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity中OnCollisionEnter内hitCount计数器不增长问题求助

问题:子弹击中后hitCount不更新,UI分数无变化

以下是我挂载在动态生成子弹实例上的Bullet.cs脚本:

using UnityEngine;

public class Bullet : MonoBehaviour
{
    public static Bullet instance;
    public float bulletSpeed = 2f;
    public float destroyTime = 2.5f;
    public int damageAmount = 10;
    public GameObject explosion;
    private int hitCount = 0;
    int fuck = 0;

    private void Awake()
    {
        instance = this;
    }

    private void Start()
    {
        Destroy(gameObject, destroyTime);
    }

    private void Update()
    {
        float distance = bulletSpeed * Time.deltaTime; // distance to move this frame
        Vector3 movement = new Vector3(distance, 0f, 0f); // movement vector
        transform.Translate(movement); // move the object
    }

    private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.CompareTag("destroyer"))
        {
            Destroy(gameObject);
        }

        if (collision.gameObject.CompareTag("Astroid"))
        {
            Destroy(gameObject); // Destroy the bullet
            Destroy(collision.gameObject); // destroy the asteroid
            hitCount += 5;
            
            //UIManager.instance.UpdateScoreText(hitCount);
            Instantiate(explosion, transform.position, Quaternion.identity);
        }
    }


    public int GetHitCount()
    {
        return hitCount;
    }
}

这是挂载在Canvas上的UIManager.cs脚本:

using TMPro;
using UnityEngine;

public class UIManager : MonoBehaviour
{
    public TMP_Text scoreText;
    public TMP_Text highScoreText;

    private int score = 0;
    
    public static UIManager instance;
    private void Awake()
    {
        instance = this;
    }

    private void Start()
    {
        scoreText.text = score.ToString();
        
    }


    public void UpdateScoreText( int hitCount)
    {
        score = hitCount;
        scoreText.text = score.ToString();
    }
}

目前Debug.log的计数器可正常工作,但hitCount数值始终保持不变,请问我哪里出错了?


问题根源

  1. hitCount是局部变量:每个子弹实例都有自己的hitCount,子弹碰撞后立刻被销毁,这个变量也跟着消失,根本无法累加全局分数。
  2. UI更新代码被注释:你把UIManager.instance.UpdateScoreText(hitCount);注释掉了,就算hitCount有值也传不到UI系统里。
  3. Bullet的单例完全无效:每次生成新子弹都会覆盖Bullet.instance,旧子弹的实例引用直接丢失,这个静态单例没有任何实际作用。

修复方案

把分数的存储逻辑移到全局唯一的UIManager中,子弹只负责通知UI加分,不要自己存分数。

修改后的Bullet.cs

using UnityEngine;

public class Bullet : MonoBehaviour
{
    public float bulletSpeed = 2f;
    public float destroyTime = 2.5f;
    public int damageAmount = 10;
    public GameObject explosion;

    private void Start()
    {
        Destroy(gameObject, destroyTime);
    }

    private void Update()
    {
        float distance = bulletSpeed * Time.deltaTime;
        Vector3 movement = new Vector3(distance, 0f, 0f);
        transform.Translate(movement);
    }

    private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.CompareTag("destroyer"))
        {
            Destroy(gameObject);
            return; // 避免执行后续逻辑
        }

        if (collision.gameObject.CompareTag("Astroid"))
        {
            Destroy(collision.gameObject);
            Instantiate(explosion, transform.position, Quaternion.identity);
            // 直接通知UI系统加分
            UIManager.instance.AddScore(5);
            Destroy(gameObject);
        }
    }
}

修改后的UIManager.cs

using TMPro;
using UnityEngine;

public class UIManager : MonoBehaviour
{
    public TMP_Text scoreText;
    public TMP_Text highScoreText;

    private int score = 0;
    
    public static UIManager instance;

    private void Awake()
    {
        // 确保UIManager全局唯一
        if (instance == null)
        {
            instance = this;
            DontDestroyOnLoad(gameObject);
        }
        else
        {
            Destroy(gameObject);
        }
    }

    private void Start()
    {
        scoreText.text = score.ToString();
        // 可在此添加读取本地高分的逻辑,比如用PlayerPrefs
    }

    // 改为加分方法,实现分数累加
    public void AddScore(int addValue)
    {
        score += addValue;
        scoreText.text = score.ToString();
        // 可在此添加更新本地高分的逻辑
    }
}

修复逻辑说明

  1. 分数存在UIManager中,全局唯一,不会因为子弹销毁而丢失。
  2. 子弹碰撞后直接调用UI的AddScore方法,每次加5分,实现分数累加。
  3. 修复了UIManager的单例逻辑,确保场景切换时也不会丢失分数(如果需要)。
  4. 移除了Bullet中无用的单例和局部hitCount,代码逻辑更简洁清晰。

内容的提问来源于stack exchange,提问作者debugsaurav

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 16:05:32