I spawn a ship object using a command and want my player have ship as a property, but I can’t access player from server.
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
namespace Assets._Scripts
{
public class PlayerController : NetworkBehaviour
{
public GameObject ShipPrefab;
public GameObject Ship;
void Start()
{
if (!isLocalPlayer)
return;
CmdCreateShip(netId);
}
void Update()
{
if (!isLocalPlayer)
return;
var shipController = Ship.GetComponent<ShipController>();
if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W))
shipController.CmdAdvance();
}
[Command]
private void CmdCreateShip(NetworkInstanceId id)
{
var ship = Instantiate(ShipPrefab);
NetworkServer.SpawnWithClientAuthority(ship, connectionToClient);
}
}
}