从购物系统获取空引用
Getting null reference from a shopping system
我再次在 Whosebug 上寻求帮助,因为我遇到了一个我无法解决的错误。对于 Context,我是 Unity 的新手,所以我遵循了一个教程。这是 link:
[https://www.youtube.com/watch?v=Oie-G5xuQNA]
为了更方便地访问代码,我还将在此处发布它:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ButtonInfo : MonoBehaviour
{
public int ItemID;
public Text PriceText;
public Text QuantityText;
public GameObject ShopManager;
// Update is called once per frame
void Update()
{
PriceText.text = "Price: $" + ShopManager.GetComponent<ShopManagerScript>().shopItems[2, ItemID].ToString();
QuantityText.text = ShopManager.GetComponent<ShopManagerScript>().shopItems[3, ItemID].ToString();
}
}
还有第二个脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class ShopManagerScript : MonoBehaviour
{
public int [,] shopItems = new int[5,5];
public float coins;
public Text CoinsText;
void Start()
{
CoinsText.text = "Coins" + coins.ToString();
// ID's
shopItems[1, 1] = 1;
shopItems[1, 2] = 2;
shopItems[1, 3] = 3;
shopItems[1, 4] = 4;
// Price
shopItems[2, 1] = 10;
shopItems[2, 2] = 20;
shopItems[2, 3] = 30;
shopItems[2, 4] = 40;
// Quantity
shopItems[3, 1] = 1;
shopItems[3, 2] = 1;
shopItems[3, 3] = 1;
shopItems[3, 4] = 1;
}
public void Buy()
{
GameObject ButtonRef = GameObject.FindGameObjectWithTag("Event").AddComponent<EventSystem>().currentSelectedGameObject;
if(coins >= shopItems[2, ButtonRef.GetComponent<ButtonInfo>().ItemID])
{
coins -= shopItems[2, ButtonRef.GetComponent<ButtonInfo>().ItemID];
shopItems[3, ButtonRef.GetComponent<ButtonInfo>().ItemID]++;
CoinsText.text = "Coins" + coins.ToString();
ButtonRef.GetComponent<ButtonInfo>().QuantityText.text = shopItems[3, ButtonRef.GetComponent<ButtonInfo>().ItemID].ToString();
}
}
}
最后一件事:这是错误消息本身:
NullReferenceException:对象引用未设置到对象的实例
ShopManagerScript.Buy () (位于 Assets/Scrips/UI/Shop System/ShopManagerScript.cs:43)
抱歉,如果粘贴了这么多代码有点乱,但我只是想让我的问题得到解答。所以,如果有人愿意帮助我,我将不胜感激! :)
在您的 Buy() 方法中,您似乎正在调用 AddComponent 以将 EventSystem 添加到标记为“事件”的对象中。很可能这个对象已经有一个 EventSystem,所以它不能添加另一个,并且它提供了对 ButtonRef 对象的空引用。
您很可能想在那里调用 GetComponent 来获取已经存在的 EventSystem 组件
我再次在 Whosebug 上寻求帮助,因为我遇到了一个我无法解决的错误。对于 Context,我是 Unity 的新手,所以我遵循了一个教程。这是 link: [https://www.youtube.com/watch?v=Oie-G5xuQNA] 为了更方便地访问代码,我还将在此处发布它:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ButtonInfo : MonoBehaviour
{
public int ItemID;
public Text PriceText;
public Text QuantityText;
public GameObject ShopManager;
// Update is called once per frame
void Update()
{
PriceText.text = "Price: $" + ShopManager.GetComponent<ShopManagerScript>().shopItems[2, ItemID].ToString();
QuantityText.text = ShopManager.GetComponent<ShopManagerScript>().shopItems[3, ItemID].ToString();
}
}
还有第二个脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class ShopManagerScript : MonoBehaviour
{
public int [,] shopItems = new int[5,5];
public float coins;
public Text CoinsText;
void Start()
{
CoinsText.text = "Coins" + coins.ToString();
// ID's
shopItems[1, 1] = 1;
shopItems[1, 2] = 2;
shopItems[1, 3] = 3;
shopItems[1, 4] = 4;
// Price
shopItems[2, 1] = 10;
shopItems[2, 2] = 20;
shopItems[2, 3] = 30;
shopItems[2, 4] = 40;
// Quantity
shopItems[3, 1] = 1;
shopItems[3, 2] = 1;
shopItems[3, 3] = 1;
shopItems[3, 4] = 1;
}
public void Buy()
{
GameObject ButtonRef = GameObject.FindGameObjectWithTag("Event").AddComponent<EventSystem>().currentSelectedGameObject;
if(coins >= shopItems[2, ButtonRef.GetComponent<ButtonInfo>().ItemID])
{
coins -= shopItems[2, ButtonRef.GetComponent<ButtonInfo>().ItemID];
shopItems[3, ButtonRef.GetComponent<ButtonInfo>().ItemID]++;
CoinsText.text = "Coins" + coins.ToString();
ButtonRef.GetComponent<ButtonInfo>().QuantityText.text = shopItems[3, ButtonRef.GetComponent<ButtonInfo>().ItemID].ToString();
}
}
}
最后一件事:这是错误消息本身: NullReferenceException:对象引用未设置到对象的实例 ShopManagerScript.Buy () (位于 Assets/Scrips/UI/Shop System/ShopManagerScript.cs:43)
抱歉,如果粘贴了这么多代码有点乱,但我只是想让我的问题得到解答。所以,如果有人愿意帮助我,我将不胜感激! :)
在您的 Buy() 方法中,您似乎正在调用 AddComponent 以将 EventSystem 添加到标记为“事件”的对象中。很可能这个对象已经有一个 EventSystem,所以它不能添加另一个,并且它提供了对 ButtonRef 对象的空引用。 您很可能想在那里调用 GetComponent 来获取已经存在的 EventSystem 组件