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

Unity初学者实现UI按钮加减分数时减法逻辑失效问题

Unity 按钮点击分数计算逻辑异常问题

问题描述

  • 功能需求:在UI文本组件上实时展示游戏分数,实现点击「OK」按钮分数+1,点击「NO」按钮分数-5的交互逻辑
  • 异常现象:连续点击10次OK按钮后分数可正常显示为10,此时点击NO按钮,文本框直接显示-5,而非现有分数扣减后应得的5;后续再次点击OK按钮,分数直接变为11,不符合扣减后累加应得的6结果

初始问题代码

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


public class Clicked : MonoBehaviour
{
    public GameObject NO;
    public GameObject OK;
    public Text Points;
    public int score;
   
    public void OnClick()
    {
        Score(1);
        OK.transform.position = new Vector2(Random.Range(90, Camera.main.pixelWidth - 90), Random.Range(90, Camera.main.pixelHeight - 90));
        double rnd = Random.Range(0, 11);
        if (rnd == 1)
        {
            NO.transform.position = new Vector2(Random.Range(90, Camera.main.pixelWidth - 90), Random.Range(90, Camera.main.pixelHeight - 90));
            NO.SetActive(true);

        }
        else
        {
            NO.SetActive(false);
        }
    }
    public void Score(int points)
    {
        if(points == 5)
        {
            score = score - 5;
            Points.text = "Punkte : "+score;
        }
        else if(points == 1)
        {
            score++;
            Points.text = "Punkte : "+score;
        }
       
    }
    public void OnClickNo()
    {
        Score(5);
        NO.SetActive(false);

    }

    void Start()
    {
        NO.SetActive(false);
      
        Points.text = "Punkte : " + score;
    }

}

修复方案

该异常由Unity Inspector面板中按钮点击事件的错误绑定导致,修复操作如下:

  • 移除所有控件的原有事件绑定,重新配置组件关联关系
  • 改为在Start生命周期方法中通过代码动态添加按钮点击监听器,避免手动拖拽绑定带来的配置错误
    修复后功能运行正常,修复后代码如下:
public class Clicked : MonoBehaviour
{
    public Text Points;
    public int score = 0;


    [SerializeField]
    private Button OK;

    [SerializeField]
    private Button NO;


    public Button SelectedButton
    {
        get; private set;
    }

    private void Start()
    {
        Points.text = "Punkte : " + score;
        NO.onClick.AddListener(() => SelectButton(NO));
        OK.onClick.AddListener(() => SelectButton(OK));
    }

    public void SelectButton(Button button)
    {
        SelectedButton = button;
        if(button == OK)
        {
            score++;
            Points.text = "Punkte : " + score;
            OK.enabled = (SelectedButton == OK);
            OK.transform.position = new Vector2(Random.Range(90, Camera.main.pixelWidth - 90), Random.Range(90, Camera.main.pixelHeight - 90));
            double rnd = Random.Range(0, 5);
            if (rnd == 1)
            {
                NO.transform.position = new Vector2(Random.Range(90, Camera.main.pixelWidth - 90), Random.Range(90, Camera.main.pixelHeight - 90));

            }
        }
        else if(button == NO)
        {
            score -= 5;
            Points.text = "Punkte : " + score;
            NO.enabled = (SelectedButton == NO);
            NO.transform.position = new Vector2(Random.Range(90, Camera.main.pixelWidth - 90), Random.Range(90, Camera.main.pixelHeight - 90));

        }


    }
}

问题更新记录:调整全部实现逻辑后功能已正常运行。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.28 05:24:14