- Home /
Unet: Pass player script component from client to server
Hi guys,
I'm trying to pass the PlayerController script to the spawned bullet to determine the next turn after it hit something. Players are always a client and server is the gameserver that it hosted somewhere else.
Playerscript has this command but it does not pass it:
CmdFireBullet(gameObject);
[Command]
private void CmdFireBullet(GameObject _gameObject)
{
bullet = (GameObject) Instantiate(bulletprefab);
bullet.GetComponent<BulletController> ().AddPlayer (_gameObject.GetComponent<PlayerController>());
NetworkServer.Spawn (bullet);
}
Bulletscript (is owned by Server)
public List<PlayerController> playerList = new List<PlayerController>();
public void AddPlayer(PlayerController pc)
{
playerList.Add(pc);
}
However if the client acts as a server and I use this "ball.GetComponent ().AddPlayer (this);" in commad it passes on both side.
The problem is that you're only adding the reference to the instance on the server. after Spawn is called and the clients instantiate their bullet versions, the info is not there because it's not a syncvar. You either let the bullet itself find the player, use a syncvar for a playerreference (NetworkIdentity), or you use Custom spawn functions to get a hold on the client instance for assigning the player from outside the bullet on the client
https://docs.unity3d.com/$$anonymous$$anual/UNetCustomSpawning.html