Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Instantiate Object in different scene upon joining game and only allow client to control it.

Discussion in 'Multiplayer' started by RedOrion, Feb 26, 2015.

  1. RedOrion

    RedOrion

    Joined:
    Nov 16, 2013
    Posts:
    2
    This code is in a different scene from the main game area. I'm trying to figure out how to get my code to spawn a object in the new scene upon joining the game. I believe everything that does that needs to be under the function lobbySpawn.

    Code (JavaScript):
    1. @script ExecuteInEditMode()
    2. var gameName: String = "GameName";
    3. var refreshing = false;
    4. var hostData: HostData[];
    5. var playerPrefab: GameObject;
    6. var create = false;
    7. var joining = false;
    8. var serverName = "";
    9. var serverInfo = "";
    10. var serverPass = "";
    11. var playerName = "";
    12. var clientPass = "";
    13. var scrollPosition: Vector2 = Vector2.zero;
    14. function Start() {
    15.      playerName = PlayerPrefs.GetString("Player Name");
    16. }
    17. function OnGUI() {
    18.      if (!Network.isClient && !Network.isServer) {
    19.          if (!create && !joining) {
    20.        
    21.              GUI.Label(Rect(Screen.width / 2 - 120, Screen.height/2 - 200, 300, 30),
    22.                  gameName);
    23.              if (GUI.Button(Rect(Screen.width / 2 - 50, Screen.height / 2, 100, 20),
    24.                  "Create Game")) {
    25.                  create = true;
    26.              }
    27.              if (GUI.Button(Rect(Screen.width / 2 - 50, Screen.height / 2 + 30, 100, 20),
    28.                  "Find Game")) {
    29.                  joining = true;
    30.                  refreshHostList();
    31.              }
    32.          }
    33.          if (create) {
    34.        
    35.              GUI.Label(Rect(Screen.width / 2 - 120, Screen.height/2 - 200, 300, 30),
    36.                  gameName);
    37.              if (GUI.Button(Rect(Screen.width / 2 - 50, Screen.height / 3 + 110, 100, 50),
    38.                  "Create")) {
    39.                  startServer();
    40.              }
    41.              GUI.Label(Rect(Screen.width / 2 - 110, Screen.height / 3, 100, 20),
    42.                  "Server Name:");
    43.              GUI.Label(Rect(Screen.width / 2 + 40, Screen.height / 3, 100, 20),
    44.                  "Password:");
    45.              GUI.Label(Rect(Screen.width / 2 - 30, Screen.height / 2 + 90, 100, 20),
    46.                  "Server Info:");
    47.              serverName = GUI.TextField(Rect(Screen.width / 2 - 120, Screen.height / 3 +
    48.                  30, 100, 20), serverName, 12);
    49.              serverPass = GUI.PasswordField(Rect(Screen.width / 2 + 20, Screen.height /
    50.                  3 + 30, 100, 20), serverPass, "*" [0], 12);
    51.              serverInfo = GUI.TextArea(Rect(Screen.width / 2 - 70, Screen.height / 2 +
    52.                  120, 150, 40), serverInfo, 35);
    53.              if (GUI.Button(Rect(Screen.width / 1.2, Screen.height / 20, 100, 20),
    54.                  "Back")) {
    55.                  create = false;
    56.              }
    57.          }
    58.          if (joining) {
    59.        
    60.              GUI.Label(Rect(Screen.width / 2 - 120, Screen.height/2 - 200, 300, 30), gameName);
    61.              if (hostData) {
    62.                  scrollPosition = GUI.BeginScrollView(Rect(Screen.width / 4, Screen.height /
    63.                      6, Screen.width / 1.5, Screen.height / 2), scrollPosition, Rect(0, 0,
    64.                      300, 1000 /* hostData.length * 30 */ ));
    65.                  GUI.Label(Rect(30, 0, 100, 20), "Game Name");
    66.                  GUI.Label(Rect(350, 0, 100, 20), "Server Info");
    67.                  GUI.Label(Rect(590, 0, 100, 20), "Player Count");
    68.                  GUI.Label(Rect(700, 0, 100, 20), "Password");
    69.                  for (var i: int = 0; i < hostData.length; i++) {
    70.                      GUI.Label(Rect(0, 30 + i * 30, 200, 22), hostData[i].gameName);
    71.                      GUI.Label(Rect(160, 30 + i * 30, 500, 22), hostData[i].comment);
    72.                      GUI.Label(Rect(610, 30 + i * 30, 100, 20), hostData[i].connectedPlayers +
    73.                          " / " + hostData[i].playerLimit);
    74.                      if (hostData[i].passwordProtected) {
    75.                          clientPass = GUI.PasswordField(Rect(680, 30 + i * 30, 100, 25), clientPass, "*" [0], 12);
    76.                      }
    77.                      if (GUI.Button(Rect(800, 30 + i * 30, 100, 25), "Join")) {
    78.                          Network.Connect(hostData[i], clientPass);
    79.                      }
    80.                  }
    81.                  GUI.EndScrollView();
    82.              }
    83.              if (!hostData) {
    84.                  GUI.Label(Rect(Screen.width / 2 - 50, Screen.height / 3, 200, 25),
    85.                      "No Games Found");
    86.                  if (GUI.Button(Rect(Screen.width / 2 - 50, Screen.height / 3 + 30, 105, 25),
    87.                      "Refresh")) {
    88.                      refreshHostList();
    89.                  }
    90.              }
    91.              if (GUI.Button(Rect(Screen.width / 1.2, Screen.height / 20, 100, 20),
    92.                  "Back")) {
    93.                  joining = false;
    94.              }
    95.          }
    96.          if (GUI.Button(Rect(Screen.width / 20, Screen.height / 20, 100, 20), "Quit")) {
    97.              Application.Quit();
    98.          }
    99.          GUI.Label(Rect(Screen.width / 2 - 35, Screen.height / 1.2 - 30, 100, 20),
    100.              "Your Name:");
    101.          playerName = GUI.TextField(Rect(Screen.width / 2 - 50, Screen.height / 1.2,
    102.              100, 20), playerName, 12);
    103.      }
    104. }
    105. function Update() {
    106.      if (refreshing) {
    107.          if (MasterServer.PollHostList().Length > 0) {
    108.              refreshing = false;
    109.              hostData = MasterServer.PollHostList();
    110.          }
    111.      }
    112. }
    113. function startServer() {
    114.      if (serverPass != "") {
    115.          Network.incomingPassword = serverPass;
    116.      }
    117.      Network.InitializeServer(15, 25001, !Network.HavePublicAddress);
    118.      MasterServer.RegisterHost(gameName, serverName, serverInfo);
    119. }
    120. function OnServerInitialized() {
    121.      DontDestroyOnLoad(transform.gameObject);
    122.      Application.LoadLevel("Main");
    123.      lobbySpawn();
    124. }
    125. function OnConnectedToServer() {
    126.      lobbySpawn();
    127. }
    128. function lobbySpawn() {
    129.      yield WaitForSeconds(0.1);
    130.      var made = Network.Instantiate(playerPrefab, Vector3.position, transform.rotation, 0);
    131.    
    132.      made.GetComponent(playerMove).playerName = playerName;
    133.      PlayerPrefs.SetString("Player Name", playerName);
    134.      if (Network.isClient) {
    135.          Destroy(this);
    136.      }
    137. }
    138. function refreshHostList() {
    139.      MasterServer.ClearHostList();
    140.      MasterServer.RequestHostList(gameName);
    141.      refreshing = true;
    142. }