我希望每次匹配时我的分数增加1,但如果id不同,分数保持为1,但如果id相同则增加

问题描述 投票:0回答:1
public class SlotScript : MonoBehaviour, IDropHander
{
    public int id;

    private float score = 0;
    public void OnDrop(PointerEventData eventData)
    {
        Debug.Log("Item Dropped");

        if(eventData.pointerDrag != null)
        {
            if (eventData.pointerDrag.GetComponent<DragAndDrop>().id == id)
            {
                eventData.pointerDrag.GetComponent<RectTransform>().anchoredPosition = this.GetComponent<RectTransform>().anchoredPosition;
                
            }
            else
            {
                eventData.pointerDrag.GetComponent<DragAndDrop>().ResetPos();
            }
            score += 1;
            Debug.Log("Score is: " + score);
            if (score > 14)
            {
                SceneManager.LoadScene(1, LoadSceneMode.Single);
            }

        }
    }
}

我检查了我的另一个脚本,它拖放了对象,但这些都不是问题。我只是想继续提高我的分数,即使 ID 号不同。

unity-game-engine
1个回答
0
投票

我决定创建另一个脚本来计算有多少个 id 满足条件 (if (eventData.pointerDrag.GetComponent().id == id)),该脚本现在可以工作。我使用列表作为使用相同脚本来计算其他 15 个对象的方法。

公共类计数器:MonoBehaviour { 公共列表槽;

void Start()
{
    
    slots = new List<SlotScript>(FindObjectsOfType<SlotScript>());
}

void Update()
{
    
    int matchedCount = CountMatchedSlots();

    
    Debug.Log("Matched Count: " + matchedCount);
}

int CountMatchedSlots()
{
    int count = 0;

    
    foreach (SlotScript slot in slots)
    {
        if (slot.IsMatched()) 
        {
            count++;
            if(count == 15)
            {
                SceneManager.LoadScene(1, LoadSceneMode.Single);
            }
        }
    }

    return count;
}

}

© www.soinside.com 2019 - 2024. All rights reserved.