Hello everyone!!! So I am soooo close to figuring this all out and making it work properly! However, it is just not there yet. I have this as my custom network manager: Code (CSharp): using UnityEngine; using System.Collections; using UnityEngine.Networking; public class MyNetworkManager : NetworkManager { [SerializeField] GameObject[] characters; private GameObject selectedChar; public void SelectCharacter(int i) { selectedChar = characters[i]; } public override void OnClientConnect(NetworkConnection conn) { Debug.LogError("OnClientConnect !"); } public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId) { if (selectedChar == null) selectedChar = characters[0]; Debug.LogError("OnServerAddPlayer"); GameObject p = (GameObject)GameObject.Instantiate(selectedChar, MyNetworkManager.singleton.GetStartPosition().position, MyNetworkManager.singleton.GetStartPosition().rotation); NetworkServer.AddPlayerForConnection(conn, p, playerControllerId); GetComponent<ChoosePlayer>().enabled = false; } } However, whatever the host (or server+player) picks for his prefab (for example picks "Warrior") then ALL players will be that prefab the host picks (Warrior) since his network manager is on "Warrior". There needs to be some way to send the server "what I want to be" before adding the player in, and I cannot figure out how to do this. Any help please?? It's almost like I need to add inside the OnClientConnect method something like: "My connection is 'conn' and please let me be 'selectedChar' " And then somehow receive that message on the server before spawning that connection's player. I don't know if this is possibly, or even how to do it..
Just an ideia, don't know if it will work, but you could send a Command ( Message from client to server only ) with the instantiate request. Note that all parameters inside a command function will have there values automatically replicated on the server. So if you call: [Command] void CmdSpawnPlayer() { //GameObject player = Set player prefab to clients choice //NetworkServer.AddPlayerForConnection(conn, p, playerControllerId); } It should spawn the right character for everyone.
Tried a few things for this to work.. Could not get it to work because the script derives from NetworkManager which cannot have "Commands". Then I tried making another script just to do this command and this will not work because it derives from NetworkBehaviour which is disabled on-load. Any other way for anyone to make this all work please?
What did you end up doing, if you dont mind me asking? I've been struggling with the same issue for multiple prefabs. currently sending Commands and RPCs to change appearance over the network but hasn't proven to be the most efficient way to solve the problem.
Whoops, wrong account. The question above is from me. Really interested to see what you ended up doing #iSleepzZz
This is HOW I do it with my current project.The comments should be enough to get an understanding. its the MESSAGE part that you are most concerned with as its what is passed from the client machine (and host/client machine) LOCALLY, then the server code takes over after OnserverAddPlayer is done executing. Hope that helps. NETWORK MANAGER CODE: Code (CSharp): //Called on client when connect public override void OnClientConnect(NetworkConnection conn) { print("THIS IS WHERE A OnClientConnect START GOES"); //Get the LOCAL player prefab you want to have for this player. (all this does is lookup my list of player prefabs INDEX and kick back the one your requesting) curPlayerPrefabIndex = KnifeFighterGameController.Instance.globalPlayerManager.MP_PirateIndexToSpawn(); // Create message to set the player IntegerMessage msg = new IntegerMessage(curPlayerPrefabIndex); // Call Add player and pass the message ClientScene.AddPlayer(conn, 0, msg); } public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId, NetworkReader extraMessageReader) { print("THIS IS WHERE A OnServerAddPlayer START GOES"); // Read client message and receive index if(extraMessageReader != null) { var stream = extraMessageReader.ReadMessage<IntegerMessage>(); curPlayerPrefabIndex = stream.value; } //Select the prefab from the spawnable objects list FROM the local machine index that we created with our message earlier tmpPrefabGO = KnifeFighterSpawnController.Instance.objectPooler.allAvailablePlayerPrefabs[curPlayerPrefabIndex]; //Spawn my player GO from the selected prefab tempPlayerGO = KnifeFighterSpawnController.Instance.MP_CreatePlayer(tmpPrefabGO); //Save the player to a list so we can do stuff to both players later KnifeFighterGameController.Instance.globalPlayerManager.MP_AddNetworkedPlayerGO(tempPlayerGO); //Init of lists used for health, avatar, manager scripts etc. KnifeFighterGameController.Instance.globalPlayerManager.MP_GetPlayerListsSetup(); // Add player object for connection NetworkServer.AddPlayerForConnection(conn, tempPlayerGO, playerControllerId); }