Search Unity

  1. Are you interested in providing feedback directly to Unity teams? Sign up to become a member of Unity Pulse, our new product feedback and research community.
    Dismiss Notice

Player2 Not Receiving an RPC

Discussion in 'Multiplayer' started by nickavv, Dec 1, 2007.

Thread Status:
Not open for further replies.
  1. nickavv

    nickavv

    Joined:
    Aug 2, 2006
    Posts:
    1,801
    Code (csharp):
    1. var redPrefab : Transform;
    2. var bluePrefab : Transform;
    3. var redSpot : GameObject;
    4. var blueSpot : GameObject;
    5. var gamesName = "Enter the name of your game!";
    6. var gamesDesc = "Enter a short description of your game!";
    7. var gameMaster = false;
    8. var gameJoiner = false;
    9. var startUp = false;
    10. var gameInPlay = false;
    11. var player1 = false;
    12. var player2 = false;
    13. var playersFull = false;
    14. var notMain = false;
    15.  
    16. function OnGUI () {
    17.     if (!gameMaster  !gameJoiner  !gameInPlay  !notMain) {
    18.         if (GUILayout.Button("Create Game")) {
    19.             gameMaster = true;
    20.         }
    21.         if (GUILayout.Button("Join Game")) {
    22.             gameJoiner = true;
    23.         }
    24.     }
    25.     if (gameMaster == true){
    26.         gamesName = GUILayout.TextField(gamesName);
    27.         gamesDesc = GUILayout.TextArea(gamesDesc);
    28.         if (GUILayout.Button ("Create Game")) {
    29.             Network.useNat = !Network.HavePublicAddress();
    30.             Network.InitializeServer(1, 25002);
    31.             MasterServer.RegisterHost("RandomBoxWithCubesGame",gamesName,gamesDesc);
    32.             gameMaster = false;
    33.             notMain = true;
    34.             player1 = true;
    35.         }
    36.     }
    37.     if (gameJoiner == true) {
    38.         var data : HostData[] = MasterServer.PollHostList();
    39.         for (var element in data) {
    40.             GUILayout.BeginHorizontal();   
    41.             var name = element.gameName + ": " + element.comment + " (" + element.connectedPlayers + " / " + element.playerLimit + ")";
    42.             GUILayout.Label(name); 
    43.             GUILayout.Space(5);
    44.             GUILayout.FlexibleSpace();
    45.             if (GUILayout.Button("Connect")) {
    46.                 Network.useNat = element.useNat;
    47.                 if (Network.useNat) {
    48.                     print("Using Nat punchthrough to connect to host");
    49.                 } else {
    50.                     print("Connecting directly to host");
    51.                     Network.Connect(element.ip, element.port);
    52.                 }
    53.                 GUILayout.EndHorizontal();
    54.             }
    55.  
    56.         }
    57.         MasterServer.RequestHostList("RandomBoxWithCubesGame");
    58.  
    59.     }
    60.     if (notMain  !playersFull) {
    61.         GUILayout.Label ("Waiting for other players to join");
    62.     }
    63.     if (playersFull  !gameInPlay) {
    64.         if (player1) {
    65.             if (GUILayout.Button("All players have joined, Start Game")) {
    66.                 startIt ();
    67.             }
    68.         } else {
    69.             GUILayout.Label("Waiting for Player 1 to start the game");
    70.         }
    71.     }
    72.     if (startUp) {
    73.         if (player1) {
    74.             Network.Instantiate(redPrefab,redSpot.transform.position,Quaternion.identity,0);
    75.         }
    76.         if (player2) {
    77.             Network.Instantiate(bluePrefab,blueSpot.transform.position,Quaternion.identity,0);
    78.         }
    79.         startUp = false;
    80.     }  
    81. }
    82.  
    83. function OnConnectedToServer () {
    84.     gameJoiner = false;
    85.     notMain = true;
    86.     player2 = true;
    87. }
    88.  
    89. function OnPlayerDisconnected (player: NetworkPlayer) {
    90.     Network.RemoveRPCs (player);
    91.     Network.DestroyPlayerObjects (player);
    92. }
    93.  
    94. function Awake () {
    95.     MasterServer.ClearHostList();
    96.     MasterServer.RequestHostList("RandomBoxWithCubesGame");
    97. }
    98.  
    99. function FixedUpdate () {
    100.     if (Network.connections.length > 0) {
    101.         playersFull = true;
    102.     }
    103. }
    104.  
    105. @RPC
    106. function startIt () {
    107.     gameInPlay = true;
    108.     startUp = true;
    109. }
    This script has come a long ways since I started. It now has nice fancy HUDs for everything. :p Now my problem is, when Player 1 presses the "All players have joined, Start Game" button, which calls the RPC function startIt, it never registers with Player2. This one is really driving me crazy. Help!

    :)
     
  2. larus

    larus

    Unity Technologies

    Joined:
    Oct 12, 2007
    Posts:
    268
    You only execute StartIt() locally, not as an RPC call, networkView.RPC("StartIt", ...)
     
  3. nickavv

    nickavv

    Joined:
    Aug 2, 2006
    Posts:
    1,801
    Aaah, I knew I was forgetting how to do RPCs! >.< Thanks! n_n
     
Thread Status:
Not open for further replies.
unityunity