- Home /
Multiple player prefabs in the network manager?
I'm currently working on a project that allows players to choose their player model (i.e. car model), but Unity's new Network Manager only allows for a single 'Player Prefab' slot.
Initially, I wrote a script that put all player models into an array and I called each one when I switched player models using UI. However, it seems that you can not dynamically switch the Network Manager's player prefab in code.
Next, I tried the terribly long and inefficient method of creating a single prefab containing every player model, and I would disable all other player models that weren't being used. This worked as well, but I could not find a way to save these enabled/disabled choices to the prefab to be used in the Network Manager.
So I'm curious if anyone else has run into this problem or if anyone has found a fix. Trust me, I'm willing to try anything at this point. Thanks a ton!
Thank you for posting this. I've been looking for an answer, and was leaning towards the second solution you mentioned, so I'm glad to know it's not worth it.
Ideally, there would be an example project of this. I've found several similar forum threads and asks, but honestly I can't understand what's being said, given my limited knowledge.
People seem to think these are solutions, so hopefully the benefit you. http://forum.unity3d.com/threads/unet-spawning-different-player-prefabs-solved.387747/ http://abrgame.blogspot.com/2016/01/using-unet-to-spawn-different-player.html?view=magazine
I have no experience working with UNet, i prefer Photon. Have you tried prefabing the type-specific stuff upon creation depending on player speific properties, like selected car, color etc?
CorePrefab[with a creation script attached]
|- SpecificStructure[created by creation script]
Answer by Tubel · Aug 21, 2016 at 08:59 AM
Hey man, I have a solution for you.
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Networking.NetworkSystem;
public class NetworkController : NetworkManager {
public Transform spawnPosition;
public int curPlayer;
//Called on client when connect
public override void OnClientConnect(NetworkConnection conn) {
// Create message to set the player
IntegerMessage msg = new IntegerMessage(curPlayer);
// Call Add player and pass the message
ClientScene.AddPlayer(conn,0, msg);
}
// Server
public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId, NetworkReader extraMessageReader ) {
// Read client message and receive index
if (extraMessageReader) {
var stream = extraMessageReader.ReadMessage<IntegerMessage> ();
curPlayer = stream.value;
}
//Select the prefab from the spawnable objects list
var playerPrefab = spawnPrefabs[curPlayer];
// Create player object with prefab
var player = Instantiate(playerPrefab, spawnPosition.position, Quaternion.identity) as GameObject;
// Add player object for connection
NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
}
}
Answer by Avedis · Mar 20, 2018 at 04:59 PM
I know it's an old post but @Tubel answer really helped me. I have 2 types of prefabs, a male and female so I need to be able to spawn either one from a login script. I have a custom network manager with overrides based on Tubel's answer, I assign curPlayer via the login script (which is only on the local clients) this in turn tells the server's network manager which prefab from the spawnable list to use for the connecting player. Thanks!
Answer by Cristian-Serrano-Martinez · Dec 09, 2018 at 02:01 AM
I have the same problem but this not works for me, another idea?
Fwiw, my advice is to switch to a different networking solution. https://support.unity3d.com/hc/en-us/articles/360001252086-UNet-Deprecation-FAQ
You might want to consider mirror (it's based on and very similar to unet hlapi, but solves many of it's problems). Also be advised, it seems photon bolt is the only option with p2p.
Hi Jinnindo! I'm using $$anonymous$$irror, but when I try to override void OnServerAddPlayer I get the error: no suitable method found to override
Just looking for any solutions to this error!