2015-06-02 80 views
-1

我試圖使我的第一個服務器/客戶端遊戲統一,並與RPC卡住。我在這裏得到一個很奇怪的空引用異常:Unity RPC空引用異常

for (int b = 1; b <= Network.connections.Length; b++) 
{ 
    playerList[b] = genPlayer(Network.connections[b-1]); 
    Debug.Log(
     "player " + b + 
     " has ip " + Network.connections[b - 1].ipAddress + 
     " port " + Network.connections[b - 1].port + 
     " with ID " + playerList[b].ID + 
     " and info: " + playerList[b].info.guid); 
    networkView.RPC("GetID", playerList[b].info, playerList[b].ID, b); 
    ResendHand(playerList[b]); 
} 

而genPlayer:

public player genPlayer(NetworkPlayer plr) 
{ 
    Debug.Log("the new player added from: " + plr.ipAddress);     
    player pl = new player(); 
    pl.ID = getID();//just a random int 
    pl.card1 = getCard(pl.ID); //random card 
    pl.card2 = getCard(pl.ID); 
    pl.card3 = getCard(pl.ID); 
    pl.card4 = getCard(pl.ID); 
    pl.card5 = getCard(pl.ID); 
    pl.score = 0; 
    pl.vote = 0; 
    pl.info = plr;    
    Debug.Log("the player was generated successfully with " + "ID " + pl.ID); 
    return pl; 
} 

這裏是信息:169.254.80.80:

新玩家加入,從

玩家成功生成ID 664652695

播放器1具有腹膜ID 664652695和信息169.254.80.80端口50571:1

的NullReferenceException ServerOperations.StartTheGame()(在 資產/ ServerOperations.cs:66)

+2

不要用[標籤:團結]關於Unity遊戲引擎的問題,標籤合一對[ Unity容器](https://msdn.microsoft.com/en-us/library/ff647202.aspx)。 –

+0

哪條線是66線? –

+1

檢查你的'networkView'對象是否爲空。 – rageit

回答

0

去在Unity3D文檔用於進行RPC調用。

http://docs.unity3d.com/ScriptReference/NetworkView.RPC.html

我看到這種格式製作的RPC調用。

public function RPC(name: string, mode: RPCMode, params args: object[]): void; 

對於RPCMode

http://docs.unity3d.com/ScriptReference/RPCMode.html

你不告訴它使用它的變量,如RPCMode.All或RPCMode之一。 AllBuffered。

不知道這是你的問題,但它確實看起來像一個。

你的正確語法應該是這樣的(假設「的getId」需要一個參數)

networkView.RPC("GetID", RPCMode.All, playerList[b].ID);