我是 C# 和 Unity 的完全初学者,并且在我的商店代码中遇到了错误。如果有人能找到错误并让我知道,我将不胜感激。另外,关于代码的任何反馈都会非常有用,因为我最近才开始使用 Unity。
我目前有一个商店布局,全部位于单独的面板中,带有 3 个信息文本框,然后是一个购买按钮。我想要这样,当你点击购买时,它会检查你是否有足够的钱,如果有的话,它会显示你想要购买它的确认,然后如果你同意,它就会购买它。
目前的问题是,每当你买东西时,它会向你收取10、20、然后30,这是3件商品的价格。我存储价格值的方式是在脚本中,该脚本被复制到每个面板中 3 次,所有面板中都有值。请帮忙
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.Common;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using Unity.VisualScripting;
public class ShopLogic : MonoBehaviour
{
GameObject shopUI;
public string itemName;
public string itemDescription;
public int itemPrice;
public bool ownsItem;
GameObject objectParent;
GameObject data;
GameObject buttonTitle;
GameObject buttonDescription;
GameObject buttonPrice;
GameObject confirmCanvas;
GameObject confirmItemTitle;
GameObject confirmDescription;
GameObject confirmButton;
public PlayerValues playerStats;
public int currentPrice;
void Start()
{
ownsItem = false;
objectParent = transform.parent.gameObject;
data = objectParent.transform.Find("Data").gameObject;
buttonTitle = objectParent.transform.Find("Title").gameObject;
buttonDescription = objectParent.transform.Find("Description").gameObject;
buttonPrice = objectParent.transform.Find("Price").gameObject;
shopUI = objectParent.transform.parent.gameObject;
confirmCanvas = transform.parent.parent.Find("ConformationTab").gameObject;
confirmButton = confirmCanvas.transform.Find("PurchaseButton").gameObject;
confirmButton.GetComponent<Button>().onClick.AddListener(ConfirmClick);
string titleText = data.GetComponent<ShopLogic>().itemName;
string descriptionText = data.GetComponent<ShopLogic>().itemDescription;
int itemPrice = data.GetComponent<ShopLogic>().itemPrice;
buttonTitle.GetComponent<TextMeshProUGUI>().text = titleText;
buttonDescription.GetComponent<TextMeshProUGUI>().text = descriptionText;
buttonPrice.GetComponent<TextMeshProUGUI>().text = itemPrice.ToString();
}
public void itemClick()
{
int itemPrice = data.GetComponent<ShopLogic>().itemPrice;
string itemName = data.GetComponent<ShopLogic>().itemName;
string itemDescription = data.GetComponent<ShopLogic>().itemDescription;
int playerCoins = playerStats.Cash;
if (playerCoins >= currentPrice)
{
Confirm(itemPrice, itemName, itemDescription);
}
else
{
Debug.Log("Insufficient coins.");
}
}
public void Confirm(int price, string itemName, string itemDescription)
{
currentPrice = price;
confirmItemTitle = confirmCanvas.transform.Find("ItemTitle").gameObject;
confirmDescription = confirmCanvas.transform.Find("ItemDescription").gameObject;
confirmItemTitle.GetComponent<TextMeshProUGUI>().text = itemName;
confirmDescription.GetComponent<TextMeshProUGUI>().text = itemDescription;
confirmCanvas.transform.SetParent(transform.parent);
confirmCanvas.SetActive(true);
}
public void ConfirmClick()
{
ownsItem = true;
playerStats.Cash -= currentPrice;
confirmCanvas.transform.SetParent(shopUI.transform);
confirmCanvas.SetActive(false);
Debug.Log("Player charged: " + currentPrice);
}
}````
基本上这个脚本就是 ShopLogic,那么为什么你继续使用 GetComponent 来获取另一个 ShopLogic 并从那里读取值呢?此外,当您将名称“itemPrice”、“itemName”、“itemDescription”声明为字段时,您会继续重复使用名称“itemPrice”、“itemName”、“itemDescription”作为局部变量,如果您不小心,这可能会很棘手并导致故障。 从这样的清理开始,检查现在是否有效
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.Common;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using Unity.VisualScripting;
public class ShopLogic : MonoBehaviour
{
GameObject shopUI;
public string itemName;
public string itemDescription;
public int itemPrice;
public bool ownsItem;
GameObject objectParent;
GameObject data;
GameObject buttonTitle;
GameObject buttonDescription;
GameObject buttonPrice;
GameObject confirmCanvas;
GameObject confirmItemTitle;
GameObject confirmDescription;
GameObject confirmButton;
public PlayerValues playerStats;
public int currentPrice;
void Start()
{
ownsItem = false;
objectParent = transform.parent.gameObject;
data = objectParent.transform.Find("Data").gameObject;
buttonTitle = objectParent.transform.Find("Title").gameObject;
buttonDescription = objectParent.transform.Find("Description").gameObject;
buttonPrice = objectParent.transform.Find("Price").gameObject;
shopUI = objectParent.transform.parent.gameObject;
confirmCanvas = transform.parent.parent.Find("ConformationTab").gameObject;
confirmButton = confirmCanvas.transform.Find("PurchaseButton").gameObject;
confirmButton.GetComponent<Button>().onClick.AddListener(ConfirmClick);
buttonTitle.GetComponent<TextMeshProUGUI>().text = itemName;
buttonDescription.GetComponent<TextMeshProUGUI>().text = itemDescription;
buttonPrice.GetComponent<TextMeshProUGUI>().text = itemPrice.ToString();
}
public void itemClick()
{
int playerCoins = playerStats.Cash;
if (playerCoins >= currentPrice)
{
Confirm(itemPrice, itemName, itemDescription);
}
else
{
Debug.Log("Insufficient coins.");
}
}
public void Confirm(int price, string confirmedItemName, string confirmendItemDescription)
{
currentPrice = price;
confirmItemTitle = confirmCanvas.transform.Find("ItemTitle").gameObject;
confirmDescription = confirmCanvas.transform.Find("ItemDescription").gameObject;
confirmItemTitle.GetComponent<TextMeshProUGUI>().text = confirmedItemName;
confirmDescription.GetComponent<TextMeshProUGUI>().text = confirmendItemDescription;
confirmCanvas.transform.SetParent(transform.parent);
confirmCanvas.SetActive(true);
}
public void ConfirmClick()
{
ownsItem = true;
playerStats.Cash -= currentPrice;
confirmCanvas.transform.SetParent(shopUI.transform);
confirmCanvas.SetActive(false);
Debug.Log("Player charged: " + currentPrice);
}
}