单击按钮游戏逻辑无法正常工作

Clicking button game logic not working properly

在我的脚本中,每当您单击 "Button" 时,您的分数就会增加 1。当您总共错过该按钮 10 次时,它会加载一个新场景并且您输掉了游戏。当您单击该按钮时,它会将总数重置为 0。我遇到的问题是,如果您错过了该按钮 9 次,然后在第 10 次单击它,它仍然计数为 10,并且它加载了新屏幕,您输了。

它应该做的是将其重置为 0,您可以继续玩。

loseObject 在游戏开始和按下按钮时将数量设置为 0。

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
[RequireComponent(typeof(AudioSource))]

public class ButtonReposition : MonoBehaviour
{
public Button prefab;
public GameObject loseObject;
private int count;
public Text countText;

// Use this for initialization

void Start()
{
    count = 0;
    SetCountText();
    float x = Random.Range(325f, -600f);
    float y = Random.Range(250f, -450f);
    Debug.Log(x + "," + y);
    prefab.GetComponent<RectTransform>().anchoredPosition = new Vector2(x, y);
    loseObject.GetComponent<Lose>().amount = 0;
}

public void Move()
{
    float x = Random.Range(325f, -600f);
    float y = Random.Range(250f, -450f);
    Debug.Log(prefab.transform.position.x + "," + prefab.transform.position.y);
    prefab.GetComponent<RectTransform>().anchoredPosition = new Vector2(x, y);
    loseObject.GetComponent<Lose>().amount = 0;
    count = count + 1;
    SetCountText();
}

void SetCountText()
{
    countText.text = "Count: " + count.ToString();
}
}

并在按下鼠标按钮时将总数加 1 并加载下一个场景。

using UnityEngine.Audio;
using UnityEngine.UI;

public class Lose : MonoBehaviour
{
public GameObject Button;
public GameObject Target;
public GameObject loseObject;
public int amount;
public ButtonReposition script;
public void ChangeScene(int changeTheScene)   

{
    SceneManager.LoadScene(changeTheScene);
}

void Start()
{

}
void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        amount += 1;
        Debug.Log(amount);
        if (amount == 10)
        {
            SceneManager.LoadScene(2);
            Destroy(Button);
        }
    }
}
}

我认为对您来说最简单的解决方法是确保您的 loseObject 脚本在您的第一个脚本之后执行。您可以使用脚本执行顺序来做到这一点。如果您转到“编辑”->“项目设置”->“脚本执行顺序”,您可以添加您的 loseObject 脚本并将其绘制在 "Default" 下方。然后您的其他脚本将首先执行并将数量设置为 0,如您所料。但是,设置为0后,loseObject仍然会加1,所以你真正想做的是设置amount为-1​​。

在其他方面,要做到这一点,您必须找到一种方法来确定鼠标按钮何时按下以及按钮何时被单击。你现在没有那样做。您现在正在做的是检查何时用 if (Input.GetMouseButtonDown(0)).

单击鼠标左键

这是其中一种情况,其中使用 Image 组件比使用 Button 组件更好。除非你真的需要这里的Button组件,否则应该使用Image组件。

这可以通过 IPointerDownHandlerIPointerUpHandler 界面及其 OnPointerDownOnPointerUp 函数来完成。

暂时禁用您的 ButtonReposition 脚本。使用下面的脚本,看看它是否符合您的预期。如果一切正常,您可以启用 ButtonReposition 脚本并删除其中不必要的内容。

将 ButtonDetector 附加到您的图像组件:

public class ButtonDetector : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
    public bool clicked = false;

    public void OnPointerDown(PointerEventData eventData)
    {
        clicked = true;
        //Debug.Log("Pointer Down!");
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        //Reset
        clicked = false;
        // Debug.Log("Pointer Up!");
    }

    // Use this for initialization
    void Start()
    {

    }
}

您的新 Lose 脚本:

public class Lose : MonoBehaviour
{
    public GameObject Button;
    public GameObject Target;
    public GameObject loseObject;


    public int missedAmount = 0;
    public int clickedAmount = 0;
    const int MISSED_MAX_AMOUNT = 10;

    //public ButtonReposition script;
    public void ChangeScene(int changeTheScene)

    {
        SceneManager.LoadScene(changeTheScene);
    }

    private ButtonDetector buttonImage;

    void Start()
    {
        GameObject obj = GameObject.Find("ButtonImage");
        buttonImage = obj.GetComponent<ButtonDetector>();
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            if (buttonImage.clicked)
            {

                Debug.Log("Mouse Clicked on Button!");
                clickedAmount++;
                //Reset missedAmount to 0
                missedAmount = 0;
            }
            else
            {
                Debug.LogWarning("Mouse Click on SOMETHING ELSE");
                missedAmount++;
            }

            if (missedAmount == 10)
            {
                Debug.LogWarning("GameOver!");
                Debug.LogWarning("Missed 10 times in a row!");
                SceneManager.LoadScene(2);
                //Destroy(buttonImage.gameObject);
            }
        }
    }
}