Search Unity

NetworkManager not spawning user data properly

Discussion in 'Multiplayer' started by bchen5803, Jan 13, 2016.

  1. bchen5803

    bchen5803

    Joined:
    May 25, 2015
    Posts:
    11
    So what I have is a menu scene where players can create their characters and save using playerprefs. then they can load the game and theyll enter the scene with networkmanager.

    When players load theyll be loading their own respective data and I've made my own network manager so that it will spawn depending on the players saved data.

    So for example if im a knight class then it will spawn a knight prefab for me
    If my friend joins and hes a mage then it will spawn a mage prefab for him.

    The problem is that the persons host becomes the prefab for everyone in the game.
    Meaning if I host the game and I'm a knight class, everyone else that joins will also be spawned as a knight and wont spawn their own class.
    I have no idea how to fix it and unitys networking documentation is a mess.
    I'm also not sure if its relevant but my playerprefs are all static variables.
    So in the code Playerdata.classname is actually a static variable.

    PLEASE HELP. spent the entire day already with no heads !

    Thank you

    Here is the NetworkHandler ( NetworkManager) for my game.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3. using System.Collections;
    4.  
    5. public class NetworkHandler :NetworkManager {
    6.  
    7.     //depending on loaded file selected class, spawn prefab according to selected class
    8.     public GameObject warriorPrefab;
    9.     public GameObject magePrefab;
    10.     public GameObject archerPrefab;
    11.     public GameObject clericPrefab;
    12.     public GameObject roguePrefab;
    13.  
    14.     //override the network manager player prefab
    15.     public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
    16.     {
    17.        
    18.         if(PlayerData.className=="Warrior")
    19.         {
    20.             var player = (GameObject)GameObject.Instantiate(warriorPrefab, new Vector3(0, 0, 0), Quaternion.identity);
    21.             NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
    22.         }
    23.  
    24.         else if(PlayerData.className=="Mage")
    25.         {
    26.             var player = (GameObject)GameObject.Instantiate(magePrefab, new Vector3(0, 0, 0), Quaternion.identity);
    27.             NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
    28.         }
    29.  
    30.         else if (PlayerData.className == "Archer")
    31.         {
    32.             var player = (GameObject)GameObject.Instantiate(archerPrefab, new Vector3(0, 0, 0), Quaternion.identity);
    33.             NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
    34.         }
    35.  
    36.         else if (PlayerData.className == "Cleric")
    37.         {
    38.             var player = (GameObject)GameObject.Instantiate(clericPrefab, new Vector3(0, 0, 0), Quaternion.identity);
    39.             NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
    40.         }
    41.  
    42.         else if (PlayerData.className == "Rogue")
    43.         {
    44.             var player = (GameObject)GameObject.Instantiate(roguePrefab, new Vector3(0, 0, 0), Quaternion.identity);
    45.             NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
    46.         }
    47.     }
    48. }
    49.  
    50.  
    51.  
    52.  
    53.  
    54.  
     
  2. EDevJogos

    EDevJogos

    Joined:
    Jul 24, 2014
    Posts:
    75
    OnServerAddPlayer is only called on the Server, so this code is never executed on the client. When a new client enters the game OnServerAddPlayer will be called since the server choose warrior a warrior prefab will be instantiate on the server and replicated to the clients.
     
  3. zory99

    zory99

    Joined:
    May 25, 2016
    Posts:
    1
  4. EDevJogos

    EDevJogos

    Joined:
    Jul 24, 2014
    Posts:
    75
    I think the problem is that PlayerData.className is getting the server PlayerData.className since onSeverAddPlayer is only called on the server, as for a solution you coul'd try to call the instatiate of the character after OnClientConnect it's a override like OnServerAddPlayer. OnClientConnect calls 2 things ClientScene.Ready(connection) and ClientScene.AddPlayer(0) here you can also pass a message that your OnServerAddPlayer will recieve, something like new StringMessage(PlayerData.className); since this time you are on the client the prefab spawned will be your clients choice, just change on the OnServerAddPlayer instead of the PlayerData, put the string message you recieved as the parameter.


    Sry. for my english, i hope you can understand.:)