C# 按钮按下脚本中的计数器在第一次按钮按下后不更新分数

C# 按钮按下脚本中的计数器在第一次按钮按下后不更新分数,c#,unity3d,C#,Unity3d,我有一个我正在制作的游戏的统一脚本,如果你按下一个按钮,那里的图像是一个特定的图像,它应该会更新分数。但是,只有在单击第一个按钮后,分数才会被更新,随后按下的按钮仍会从按钮中删除图像,但分数不会被更新。我不知道这里发生了什么。以下是我正在使用的脚本: 顺便说一下,有9个按钮,它们都被分配了相同的脚本。我不确定这是否重要 using System.Collections; using System.Collections.Generic; using UnityEngine.UI; using U

我有一个我正在制作的游戏的统一脚本,如果你按下一个按钮,那里的图像是一个特定的图像,它应该会更新分数。但是,只有在单击第一个按钮后,分数才会被更新,随后按下的按钮仍会从按钮中删除图像,但分数不会被更新。我不知道这里发生了什么。以下是我正在使用的脚本:

顺便说一下,有9个按钮,它们都被分配了相同的脚本。我不确定这是否重要

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

public class UpdateImage : MonoBehaviour {

    public Button button;
    public Sprite noImg;
    public Sprite nug;
    public Text score;
    public int score_int = 0;

    // Use this for initialization
    void Start () {
        button = GetComponent<Button>();
        button.onClick.AddListener(UpdateImageTask);
    }

    void UpdateImageTask()
    {
        //button = GetComponent<Button>();
        if(button.GetComponent<Image>().sprite == nug)
        {
            score_int++;
            score.text = score_int.ToString();
        }
        button.GetComponent<Image>().sprite = noImg;

    }

    // Update is called once per frame
    void Update () {

    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine.UI;
使用UnityEngine;
公共类更新图像:MonoBehavior{
公共按钮;
公共雪碧;
公共精神核;
公共文本分数;
公共整数分数_int=0;
//用于初始化
无效开始(){
button=GetComponent();
button.onClick.AddListener(UpdateImageTask);
}
void UpdateImageTask()
{
//button=GetComponent();
if(button.GetComponent().sprite==nug)
{
分数μint++;
score.text=score_int.ToString();
}
button.GetComponent().sprite=noImg;
}
//每帧调用一次更新
无效更新(){
}
}

您的问题没有很好地描述,但让我试着强调一下

下面说,检查
按钮.GetComponent().sprite
是否等于
nug
,如果是,则增加
score\u int
,并用新的
score\u int
替换
score\u text
。然后,将图像
按钮.GetComponent().sprite
替换为
noImg

void UpdateImageTask()
{
    //button = GetComponent<Button>();
    if(button.GetComponent<Image>().sprite == nug)
    {
        score_int++;
        score.text = score_int.ToString();
    }
    button.GetComponent<Image>().sprite = noImg;

}
void updateImage任务()
{
//button=GetComponent();
if(button.GetComponent().sprite==nug)
{
分数μint++;
score.text=score_int.ToString();
}
button.GetComponent().sprite=noImg;
}

注意:即使未处理
if
语句,图像仍将保持为
noImg
。因此,由于
if
语句是涉及更新分数的药剂,因此它不会起作用。第一次单击按钮后,
if
语句将始终返回false,因为您已将图像从
nug
更改为
noImg
。因此,要使其工作,必须有一个将图像更改回
nug
的过程,以便
if
语句可以运行并更新分数。

我已经解决了这个问题。问题是,当您将文件发布到每个按钮时,score_int不会共享。每次只能将分数增加到1。要解决这个问题,您可以从文本框中解析分数并增加分数

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



public class UpdateImage : MonoBehaviour {

    public Button button;
    public Sprite noImg;
    public Sprite nug;
    public Text score;
    public int score_int = 0;
    // Use this for initialization
    void Start () {
        button = GetComponent<Button>();
        button.onClick.AddListener(UpdateImageTask);
    }

    void UpdateImageTask()
    {
        //button = GetComponent<Button>();
        if(button.GetComponent<Image>().sprite == nug)
        {
            score_int = System.Int32.Parse(score.text);
            score_int++;
            score.text = score_int.ToString();
        }
        button.GetComponent<Image>().sprite = noImg;

    }

    // Update is called once per frame
    void Update () {

    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine.UI;
使用UnityEngine;
公共类更新图像:MonoBehavior{
公共按钮;
公共雪碧;
公共精神核;
公共文本分数;
公共整数分数_int=0;
//用于初始化
无效开始(){
button=GetComponent();
button.onClick.AddListener(UpdateImageTask);
}
void UpdateImageTask()
{
//button=GetComponent();
if(button.GetComponent().sprite==nug)
{
score\u int=System.Int32.Parse(score.text);
分数μint++;
score.text=score_int.ToString();
}
button.GetComponent().sprite=noImg;
}
//每帧调用一次更新
无效更新(){
}
}
如果要在某一类型的所有实例之间“共享”一个值,则需要一个字段:


现在,按钮的每个实例的值都是相同的,而不必来回解析任何内容

我还在编写生成图像的代码。问题是我的score_int变量在每个脚本中都是不同的,所以在我将它们全部按下后,它们都等于1?我没有代码来测试在按下图像后发生的事情。这需要一些时间,所以我现在只是想让分数发挥作用。好吧,我的答案解释了这一点。分数仅增加到1,因为第一次按下图像时为nug,然后更改为noImg。因此,当您再次按下按钮时,if语句将不会运行,因为图像不再等于nug,因此分数不会增加,并将保持为1。直到图像再次等于nug。这个问题是关于C#,而不是(为Unity创建的Javascript派生)。
public static int score_int = 0;