【问题标题】:Counter in script for Button press not updating the score after first Button press按钮按下脚本中的计数器在第一次按下按钮后不更新分数
【发布时间】:2019-11-26 16:10:48
【问题描述】:

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

顺便说一下,有 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 () {

    }
}

【问题讨论】:

  • 这个问题是关于 C#,而不是 UnityScript(为 Unity 创建的 Javascript 衍生产品)。

标签: c# unity3d


【解决方案1】:

您的问题没有很好地描述,但让我尝试强调一些内容。

下面说,检查button.GetComponent&lt;Image&gt;().sprite 是否等于nug,如果是则增加score_int,并用新的score_int 替换score_text。然后,将图像button.GetComponent&lt;Image&gt;().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;

}

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

【讨论】:

  • 我仍在研究生成图像的代码。问题是我的 score_int 变量在每个脚本中都不同,所以在我将它们全部按下后它们都等于 1?我没有代码来测试我按下图像后会发生什么。这需要一些时间,所以我现在只是想让分数发挥作用。
  • 好吧,我的回答解释了这一点。分数只增加到1,因为第一次按图像是nug,然后更改为noImg。所以当你再次按下按钮时,if语句将不会运行,因为图像不再等于nug,因此分数不会增加并保持为1。直到图像再次等于nug。
【解决方案2】:

我已经解决了这个问题。问题是当您将文件发布到每个按钮时, 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 () {

    }
}

【讨论】:

    【解决方案3】:

    如果您希望某个类型的所有实例之间“共享”一个值,那么您需要的是 static 字段:

    public static int score_int = 0;
    

    现在,对于每个按钮实例,此值始终相同,无需来回解析任何内容!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-04
      • 2021-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-23
      相关资源
      最近更新 更多