【问题标题】:Unity RPC Null reference exception [duplicate]Unity RPC空引用异常[重复]
【发布时间】:2015-06-02 12:59:32
【问题描述】:

我正在尝试在 unity 上制作我的第一个服务器/客户端游戏,但被 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 () (在 资产/ServerOperations.cs:66)

【问题讨论】:

  • 不要使用unity 来回答有关 Unity 游戏引擎的问题,Unity 的标签是针对 Microsoft 的 Unity Container
  • 第 66 行是哪一行?
  • 检查您的networkView 对象是否为空。
  • 第 66 行:networkView.RPC("GetID", playerList[b].info, playerList[b].ID, b);
  • 这一点也不奇怪。您显然没有在代码上使用调试器。

标签: c# unity3d nullreferenceexception rpc


【解决方案1】:

查看 Unity3D 文档以进行 RPC 调用。

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

我看到了这种用于进行 RPC 调用的格式。

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

对于 RPC 模式

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

您不会告诉它使用其中一个变量,例如 RPCMode.All 或 RPCMode。全部缓冲。

不确定这是否完全是您的问题,但看起来确实是一个问题。

您的正确语法应该是这样的(假设“GetID”采用一个参数)

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多