Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Spawn Player in new scene

Discussion in 'Multiplayer' started by luckie12, Nov 22, 2016.

  1. luckie12

    luckie12

    Joined:
    Aug 17, 2014
    Posts:
    165
    Hi!

    When i connect to my server it spawns the player, but not in the scene it has to be spawned in.

    I have this in my browser:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System;
    5. using UnityEngine.SceneManagement;
    6. using UnityEngine.Networking;
    7.  
    8. public class ServerList : MonoBehaviour {
    9.  
    10.     public GameObject player;
    11.     private Rect windowRect = new Rect(100, 0, 400, 400);
    12.  
    13.     void Start()
    14.     {
    15.         MasterServer.ipAddress = "127.0.0.1";
    16.         MasterServer.port = 23466;
    17.         Network.natFacilitatorIP = "127.0.0.1";
    18.         Network.natFacilitatorPort = 50005;
    19.     }
    20.     void OnGUI()
    21.     {
    22.         windowRect = GUI.Window(0, windowRect, windowServers, "Servers");
    23.     }
    24.  
    25.     void windowServers(int id)
    26.     {
    27.  
    28.         if (GUILayout.Button("Refresh"))
    29.         {
    30.             MasterServer.RequestHostList("TESTGINGEDEVIL");
    31.         }
    32.         GUILayout.BeginHorizontal();
    33.  
    34.         GUILayout.Box("Server Name");
    35.  
    36.         GUILayout.EndHorizontal();
    37.  
    38.         if (MasterServer.PollHostList().Length != 0)
    39.         {
    40.             HostData[] data = MasterServer.PollHostList();
    41.             foreach (HostData c in data)
    42.             {
    43.                 GUILayout.BeginHorizontal();
    44.                 GUILayout.Box(c.gameName + "" + c.connectedPlayers  + " / " + c.playerLimit);
    45.                 if (GUILayout.Button("Connect"))
    46.                 {
    47.                     Network.Connect(c);
    48.                     Debug.Log(c.gameName);
    49.                     Debug.Log(c.useNat);
    50.                     Debug.Log(c.port);
    51.                     Debug.Log(c.guid);
    52.                 }
    53.                 GUILayout.EndHorizontal();
    54.             }
    55.         }
    56.  
    57.     }
    58.  
    59.  
    60.     void OnConnectedToServer()
    61.     {
    62.         SpawnPlayer();
    63.         Debug.Log("HAHA HEY");
    64.     }
    65.  
    66.     private void SpawnPlayer()
    67.     {
    68.         Network.Instantiate(player, new Vector3(0f, 5f, 0f), Quaternion.identity, 0);
    69.     }
    70. }
    71.  
    72.  
    My network manager:



    And this is how it goes:



    It spawns 2 copies of the FPSController too <-- Because its not being disconnected properly, and it does not load the right scene, the right scene is the BaseBuilder scene, which has a plane in it, where it should spawn on...
    How do i fix this?

    Thanks!
     
  2. romatallinn

    romatallinn

    Joined:
    Dec 26, 2015
    Posts:
    161
    At first, it spawns 2 players, because you assigned the player prefab (with Auto Create Player checkbox) in Network Manager component. It means the player will be spawned automatically + you spawned one player more manually.

    About the scene management... It actually should load another scene automatically. I dont see any errors. Maybe the first error caused this one. Try to fix it.
     
  3. luckie12

    luckie12

    Joined:
    Aug 17, 2014
    Posts:
    165
    Yeah i found out haha. i removed that and now it does not spawn at all haha. Heres an fast example of what happens:


    So, when i start and connect, it shows me all the server info, but it does not actually spawn my player in the Online Scene, it does not switch to the online scene at all...