我試圖讓我的遊戲中的客戶購買物品並讓這些物品通過網絡產生。爲此,我使用兩個腳本,一個播放器上的腳本和一個購買項目按鈕上的腳本。我知道NetworkServer.Spawn()只能在服務器上調用,所以我在我的播放器腳本上創建了一個命令函數來產生我的項目,而不是在購買項目腳本上進行。但是,當使用此工作時,我得到一個NullReferenceException錯誤說我傳遞的參數爲null。爲什麼參數爲空?從客戶端發送到服務器時,參數變爲空
購買物品腳本代碼:
void BuyItemAndSubtractGold(){
GameObject item = SearchForItem();
int cost = item.GetComponent<ItemManager_ItemStats>().item.buyValue;
if (item.GetComponent<ItemManager_ItemStats>() != null && storeMaster.gold >= cost) {
GameObject newItem = (GameObject)Instantiate (item);
playerMaster.CmdSpawnOnServer (newItem);
newItem.transform.parent = playerInventory.transform;
newItem.SetActive (false);
Vector3 gunPosition = newItem.GetComponent<ItemManager_ItemPickup>().gunPositions [0];
newItem.transform.localPosition = gunPosition;
newItem.transform.rotation = Quaternion.Euler(itemThrow.originalRotation);
storeMaster.gold -= cost;
audioSource.clip = boughtItem;
audioSource.Play();
storeMaster.CallEventUpdateUI();
sellInventory.LoadInventory();
playerMaster.CmdCallEventInventoryChanged();
} else {
noGold.SetActive (true);
StartCoroutine (wait5sec());
}
}
播放器的腳本代碼:
[Command]
public void CmdSpawnOnServer(GameObject item){
print (item != null);
NetworkServer.Spawn (item);
}
您可能會注意到當系統進入尋找'NetworkIdentity'分量'NullReferenceException'發生。它看起來像你試圖繞過客戶機 - 服務器體系結構,但這可能不是一個好方法。建議檢查教程結束。 – rutter
@rutter你能推薦我一個很好的教程嗎? –