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

Unity非Update方法更新分数仅单次生效的问题求助

问题根源分析
  1. 分数变量作用域错误:你的scoreBlue和scoreRed是Bullet类的实例变量,每个blue_ball子弹对象都拥有独立的变量副本。每次碰撞后子弹被Destroy(gameObject)销毁,新生成的子弹会重新初始化这两个变量为0,导致每次加10后只能显示10,无法累加。
  2. 条件判断标签不匹配:代码中第二个判断分支的标签"blue"和"red_blue_rectangle"与你描述的场景(blue_ball碰red_rectangle)不符,会导致触发逻辑失效。

修正方案

方案一:静态变量快速修复(适合快速验证)

将分数变量改为静态变量,让所有Bullet实例共享同一组分数值,不会随单个子弹销毁而重置:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bullet : MonoBehaviour
{
    // 静态变量:所有Bullet实例共享,不会随单个对象销毁重置
    private static float scoreBlue = 0;
    private static float scoreRed = 0;

    public void BlueGain()
    {       
        scoreBlue += 10;
        Debug.Log("SCORE--BLUE: " + scoreBlue);
    }

    public void RedGain()
    {         
        scoreRed += 10;
        Debug.Log("SCORE--RED: " + scoreRed);
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        // 修正标签匹配逻辑,与你的场景对齐
        if (gameObject.tag == "blue_ball" && collision.gameObject.tag == "blue_rectangle")
        {
            BlueGain();
        }
        else if (gameObject.tag == "blue_ball" && collision.gameObject.tag == "red_rectangle")
        {
            RedGain();
        }

        if (collision != null)
        {
            Destroy(gameObject); 
            Destroy(collision.gameObject); 
        }
    }
}

方案二:独立分数管理器(规范写法,适合后续扩展)

创建一个专门的GameManager脚本挂载到永久存在的空对象上,统一管理分数,避免与子弹逻辑耦合:

1. 创建GameManager脚本

using UnityEngine;

public class GameManager : MonoBehaviour
{
    // 单例模式:方便其他脚本直接调用
    public static GameManager Instance;

    public float scoreBlue = 0;
    public float scoreRed = 0;

    private void Awake()
    {
        // 确保全局只有一个GameManager实例,切换场景也不销毁
        if (Instance == null)
        {
            Instance = this;
            DontDestroyOnLoad(gameObject);
        }
        else
        {
            Destroy(gameObject);
        }
    }

    public void AddBlueScore()
    {
        scoreBlue += 10;
        Debug.Log("SCORE--BLUE: " + scoreBlue);
    }

    public void AddRedScore()
    {
        scoreRed += 10;
        Debug.Log("SCORE--RED: " + scoreRed);
    }
}

2. 修改Bullet脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bullet : MonoBehaviour
{
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (gameObject.tag == "blue_ball" && collision.gameObject.tag == "blue_rectangle")
        {
            GameManager.Instance.AddBlueScore();
        }
        else if (gameObject.tag == "blue_ball" && collision.gameObject.tag == "red_rectangle")
        {
            GameManager.Instance.AddRedScore();
        }

        if (collision != null)
        {
            Destroy(gameObject); 
            Destroy(collision.gameObject); 
        }
    }
}

额外注意事项
  • 确保游戏对象的标签设置准确:blue_ball、blue_rectangle、red_rectangle,不要出现拼写错误。
  • 碰撞对象需满足:至少一个带有Rigidbody2D组件,且碰撞体勾选Is Trigger,否则OnTriggerEnter2D不会触发。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 20:39:19