OnConnectedToServer called multiple times

Hey guys,

I’m creating a client/server game where one of the players act as a server and the other as a client.

I have one GameObject that is not destroyed between scenes and contains all RPC functions. I DO NOT create and sync it with NetworkView. I just call RPCs between client and server.

Here is the situation I’m getting into:

  • Server hosts a game to MasterServer
  • Client see it and connect to it
  • Game starts
  • Server dies (the app is closed) - All clients return to List of games scene
  • SAME SERVER PLAYER launches and hosts the app again
  • Clients see the new game, connect again
  • ------- Each client’s OnConnectedToServer is now called twice

If the server dies again, host another game and the client connect again, now the OnConnectedToServer is called three times!

Am I missing something? Do I have to call something to clear… something?

I tried the following:

private void OnDisconnectedFromServer()
{
    Network.RemoveRPCs(Network.player, 0);
    Network.RemoveRPCs(Network.player, 1);
    Network.RemoveRPCs(Network.player, 2);
    Network.RemoveRPCs(Network.player);
    Network.RemoveRPCs(this.networkView.viewID);
    Network.RemoveRPCsInGroup(0);
    Network.RemoveRPCsInGroup(1);
    Network.RemoveRPCsInGroup(2);
    Network.DestroyPlayerObjects(Network.player);
    StartCoroutine(LoadScene("Title"));
    Network.SetLevelPrefix(0);
}

None of this does anything.

Thank you in advance.

“GameObject that is not destroyed between scenes”. Is it possible that this Gameobject is in a scene which you load again? in this case you will have the gameobject several times. DontDestroyOnLoad just prevents that the object is destroyed. When you load the scene again you will end up with another GameObject.

If you want to use such a manager object, the best thing you could do is create a loader scene (which should be the first scene) and place your GameObject in there. NEVER load this scene again. All you do in Awake of the Manager script is loading the mainmenu scene (or what ever).