- Home /
Different Player Colours (Multiplayer)
Hello! I try to make a simple Multiplayer FPS Game with up to 6 Players. I have a simple spawner but I want to have a spawner which spawns players with different colours.(So 6 different colours) Here is my spawner
using UnityEngine;
using gui = UnityEngine.GUILayout;
public class GameMenu : MonoBehaviour
{
public GameObject PlayerPrefab;
string ip = "127.0.0.1";
public void Awake()
{
Application.runInBackground = true;
}
public void CreatePlayer()
{
connected = true;
var g = (GameObject)Network.Instantiate(PlayerPrefab, transform.position, transform.rotation, 1);
g.networkView.stateSynchronization = NetworkStateSynchronization.Unreliable;
g.camera.enabled = true;
camera.enabled = false;
}
void OnDisconnectedFromServer()
{
connected = false;
}
void OnPlayerDisconnected(NetworkPlayer pl)
{
Network.DestroyPlayerObjects(pl);
}
void OnConnectedToServer()
{
CreatePlayer();
}
void OnServerInitialized()
{
CreatePlayer();
}
bool connected;
void OnGUI()
{
if (!connected)
{
ip = gui.TextField(ip);
if (gui.Button("connect"))
{
Network.Connect(ip, 5300);
}
if (gui.Button("start"))
{
Network.InitializeServer(10, 5300, false);
}
}
}
}
Should I add 6 different player prefabs to the project? If yes, how do I let them spawn in a row, if the first player connects the (blue) player 1 spawns? Can somebody help me out?
Answer by ExTheSea · Jun 02, 2012 at 02:28 PM
You could probably use Network.connections.length with the 6 prefabs.
It would be something like this I think:
public GameObject FirstPrefab;
public GameObject SecondPrefab;
...
void OnConnectedtoServer(){
switch(Network.connections.length){
case(1)
CreatePlayer(SecondPrefab);//you would have chance the function to CreatePlayer(GameObject PlPref)...
case(2)
CreatePlayer(ThirdPrefab);
....
}
void OnServerInitialized()
{
CreatePlayer(FirstPrefab);
}
Maybe you don't even need to make 6 prefab. Maybe you can change between different materials/textures.
I hope my answer gives you an idea.
Yes thats an good idea, but isn't i a fault to have more than one CreatePlayer?
what do you mean? CreatePlayer(...) gets only called ones per player who connects to the server. Do you know how switch-case works?
I mean need to change your CreatePlayer to something like:
CreatePlayer(GameObject PlPref)
PlPref is just a variable in this case so you can access the different prefabs when you instantiate the player. Do you understand what i want to say?
Your answer
![](https://koobas.hobune.stream/wayback/20220613072248im_/https://answers.unity.com/themes/thub/images/avi.jpg)