我试图让我的游戏中的客户购买物品并让这些物品通过网络产生。为此,我使用两个脚本,一个播放器上的脚本和一个购买项目按钮上的脚本。我知道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你能推荐我一个很好的教程吗? –