Unet- Spawning different types of players

Hello,

I wanted to know how can I spawn different types of a player prefab? For example, I have one player (human) and zombie playable prefab, and I want to spawn 2 each type to play online. Two players are human and 2 are zombies.

I see in the network manager script there is only one prefab that can be placed. Can I make an array or list to randomly spawn different player types?

Thanks,

John

Yes you can!

You need to create your own NetworkManager. So create a script, call it CustomNetworkManger or something, and have it extend NetworkManager. (Don’t forget using UnityEngine.Networking). For example, like this:

using UnityEngine;
using UnityEngine.Networking;

public class CustomNetworkManager : NetworkManager {

}

Now, if you attach this script to an empty game object, you will see that it behaves just like the regular NetworkManager. With the key difference being that you are now in charge of what happens.

In your case, you would need to add some new variables and override some standard functions.

public GameObject humanPlayerPrefab;
public GameObject zombiePlayerPrefab;

public void OnClientConnect(Networking.NetworkConnection conn)
{
    //Your custom player spawning logic
    //Decide here who is a zombie and who is a human, etc.
}

If you glance at the documentation, you can check out what other functions you can override, and what they are supposed to do normally. Its usually always a good idea to make sure your custom function does the same thing too, in addition to your extra logic. Otherwise the NetworkManager might no longer function properly. (Like if you don’t add in player spawn logic to the above function, you will never have players spawning).

In this case, you have to make sure that your version of the OnClientConnect() function “sets the client as ready and adds a player”. That’s what the documentation says, anyway. (Unity’s networking documentation is rather bad and unreliable from my experience).