- Home /
Hey guys, I need help with character selection in 2D multiplayer game ,Hey guys, I need help with character selection in a multiplayer game
The unity network manager accepts only one player prefab however, I want the user to be able to pick from a number of characters. For now I have filled in the player prefab with a canvas that has two buttons, which upon clicking spawns in the respective character. The code attached on the canvas game object is as follows:-
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking;
public class PlayerSelect : NetworkBehaviour { [SerializeField] GameObject Deep; [SerializeField] GameObject Bandit;
public void DeepButton()
{
Cmd_NewPlayer(Deep);
}
public void BanditButton()
{
Cmd_NewPlayer(Bandit);
}
[Command]
void Cmd_NewPlayer(GameObject obj)
{
GameObject go = Instantiate(obj, obj.transform.position, Quaternion.identity); //This line throws NullReferenceException
NetworkServer.Spawn(go);
if (NetworkServer.ReplacePlayerForConnection(connectionToClient, go, playerControllerId))
Destroy(gameObject);
}
}
However when I run the game on multiple windows, this code works correctly only for the host, but when I click either buttons on the client's side I get a null reference exception saying the game object has not been assigned(I have marked the line for your reference).
Could someone please help me out in identifying the issue with this technique or is there a better way to go about player selection in a multiplayer game, if so please let me know. Thank you. ,
Your answer