2015-06-02 81 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);