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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Spawn a player for a multiplayer networked game

Discussion in 'Multiplayer' started by Deleted User, Sep 16, 2015.

  1. Deleted User

    Deleted User

    Guest

    Hi, I have been working on this for a while, and when I finally press the "spawn" button for spawnplayer function, it does not load my prefab FPSController (renamed FPSPlayer) in the prefabs folder to make myself appear in the scene. I have just a terrain and trying to get the spawnPlayer() to work. .I would be extremely grateful if anyone knows how. Also, if anyone can provide me with a good search term for google better than "Unity Multiplayer photon tutorial game", something better or more specific, for parts of that kind of process, which I don't even know what the steps are. I just need a good search term, more specific ones. All I get are broken code tutorials on youtube. not compatible with my version 5.0 of unity

    here is the code. thanks.

    Code (csharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5. public class NetworkManager2 : MonoBehaviour
    6. {
    7.    
    8.     string registeredGameName = "Game World Elements";
    9.     bool isRefreshing = false;
    10.     float refreshRequestLength = 3.0f;
    11.     HostData[] hostData;
    12.    
    13.     private void StartServer()
    14.     {
    15.         Network.InitializeServer(16, 25002, false);
    16.         MasterServer.RegisterHost(registeredGameName, "Game World Elements", "Test world.");
    17.     }
    18.    
    19.     void OnServerInitialized()
    20.     {
    21.         Debug.Log("Server has been initialized.");
    22.     }
    23.    
    24.     void OnPlayerDisconnected(NetworkPlayer player)
    25.     {
    26.         Debug.Log("Player disconnected.");
    27.         Network.RemoveRPCs(player);
    28.         Network.DestroyPlayerObjects(player);
    29.     }
    30.    
    31.     void OnApplicationQuit()
    32.     {
    33.         if (Network.isServer)
    34.         {
    35.             Network.Disconnect(200);
    36.             MasterServer.UnregisterHost();
    37.         }
    38.        
    39.         if (Network.isClient)
    40.         {
    41.             Network.Disconnect(200);
    42.         }
    43.     }
    44.    
    45.     void OnMasterServerEvent(MasterServerEvent masterServerEvent)
    46.     {
    47.         if (masterServerEvent == MasterServerEvent.RegistrationSucceeded)
    48.         {
    49.             Debug.Log("Registration Successful.");
    50.         }
    51.     }
    52.    
    53.     public IEnumerator RefreshHostList()
    54.     {
    55.         Debug.Log("Refreshing...");
    56.         MasterServer.RequestHostList(registeredGameName);
    57.         float timeStarted = Time.time;
    58.         float timeEnd = Time.time + refreshRequestLength;
    59.        
    60.         while (Time.time < timeEnd)
    61.         {
    62.             hostData = MasterServer.PollHostList();
    63.             yield return new WaitForEndOfFrame();
    64.         }
    65.        
    66.         if (hostData == null || hostData.Length == 0)
    67.         {
    68.             Debug.Log("No active servers have been found.");
    69.         }
    70.         else
    71.         {
    72.             Debug.Log(hostData.Length + " have been found.");
    73.         }
    74.        
    75.     }
    76.  
    77.       /*////////////////////////////////////////////////////////I NEED TO GET THIS TO WORK /////////////////*/
    78.  
    79.     private void SpawnPlayer()
    80.     {
    81.         Debug.Log("Spawning player...");
    82.         Network.Instantiate(Resources.Load("Prefabs/FPSPlayer"), new Vector3(0f, 2.5f, 0f), Quaternion.identity, 0);
    83.     }
    84.    
    85.    
    86.    
    87.     public void OnGUI()
    88.     {
    89.        
    90.         if (Network.isServer)
    91.         {
    92.             GUILayout.Label("Running as a server.");
    93.         }
    94.         else if (Network.isClient)
    95.         {
    96.             GUILayout.Label("Running as a client.");
    97.         }
    98.        
    99.         if (Network.isClient)
    100.         {
    101.             if (GUI.Button(new Rect(25f, 25f, 150f, 30f), "Spawn"))
    102.             {
    103.                 SpawnPlayer();
    104.             }
    105.         }
    106.         if (Network.isClient || Network.isServer)
    107.         {
    108.             return;
    109.         }
    110.        
    111.         if (GUI.Button(new Rect(25f, 25f, 150f, 30f), "Start New Server"))
    112.         {
    113.             StartServer();
    114.         }
    115.         if (GUI.Button(new Rect(25f, 65f, 150f, 30f), "Refresh Server List"))
    116.         {
    117.             StartCoroutine("RefreshHostList");
    118.         }
    119.         if (hostData != null)
    120.         {
    121.             for (int i = 0; i < hostData.Length; i++)
    122.             {
    123.                 if (GUI.Button(new Rect(Screen.width / 2, 65f + (30f * i), 300f, 30f), hostData.gameName))
    124.                 {
    125.                     Network.Connect(hostData);
    126.                 }
    127.             }
    128.         }
    129.     }
    130. }
    131.  
    132.  
     
    Last edited by a moderator: Sep 16, 2015
  2. awaterpistol

    awaterpistol

    Joined:
    Sep 3, 2015
    Posts:
    25
    "Resources.Load("Prefabs/FPSPlayer")" I'm not sure if this code would work.

    Are you not better creating a public gameobject linking and linking the prefab in the editor? Then instantiating that gameobject variable?