-1

I'm trying to make my first server/client game on unity, and get stuck with RPC. I'm getting a very strange Null reference exception here:

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]);
}

while the 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;
}

And here are messages:

the new player added from: 169.254.80.80

the player was generated successfully with ID 664652695

player 1 has ip 169.254.80.80 port 50571 with ID 664652695 and info: 1

NullReferenceException ServerOperations.StartTheGame () (at Assets/ServerOperations.cs:66)

John Saunders
  • 160,644
  • 26
  • 247
  • 397
AnK
  • 1
  • 2

1 Answers1

0

Going over the Unity3D docs for making an RPC call.

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

I see this format for making an RPC call.

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

For RPCMode

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

You don't tell it to use one of its variables, such as RPCMode.All or RPCMode. AllBuffered.

Not sure if that is your issue exactly but it certainly seems like one.

Your correct syntax should be something like this (assuming "GetID" takes one parameter)

networkView.RPC("GetID", RPCMode.All, playerList[b].ID);
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Nonlin
  • 550
  • 1
  • 6
  • 18