Unity RPC Null引用异常

时间:2015-06-02 12:59:32

标签: c# unity3d nullreferenceexception rpc

我正试图让我的第一个服务器/客户端游戏统一,并陷入RPC。我在这里得到一个非常奇怪的Null引用异常:

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有ip 169.254.80.80端口50571,ID为664652695,信息:1

     

NullReferenceException ServerOperations.StartTheGame()(at   资产/ ServerOperations.cs:66)

1 个答案:

答案 0 :(得分: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。

不确定这是不是你的问题,但它肯定是一个问题。

你的正确语法应该是这样的(假设&#34; GetID&#34;接受一个参数)

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