使用此脚本我可以将射弹从服务器射到客户端。但是当我尝试在客户端拍摄射弹时,它会显示空引用错误。有人能帮我吗? 这是错误:
NullReferenceException:对象引用未设置为对象的实例NetworkPlayerManager.CmdFire()(在Assets / Networked Scripts / NetworkPlayerManager.cs:83)NetworkPlayerManager.InvokeCmdCmdFire
脚本如下:
public class NetworkPlayerManager : NetworkBehaviour {
#region Variables
public GameObject Ball;
public float Power = 10f;
public PlayerController activePlayer;
public PlayerController player1,player2,player3;
public Renderer player1color,player2color,player3color;
public Transform PlayerFormation;
public Button b1,b2,b3;
public Canvas myCanvas;
public Camera myCamera;
private int playerNumber;
private Transform obj;
private string targetTag="Target";
#endregion
#region Built-in Functions
void Start ()
{
if (!isLocalPlayer)
{
AssignTags();
transform.name="Opponent Player- Not Local Player";
return;
}
}
void Update ()
{
if (!isLocalPlayer)
return;
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit = new RaycastHit();
if (Physics.Raycast(ray, out hit))
{
if (hit.transform.tag==targetTag&&!hit.transform.GetComponent<PlayerController>().is_hit)
{
obj=hit.transform;
CmdFire();
PlayerSelector();
}
}
}
}
#endregion
#region Override
public override void OnStartLocalPlayer()
{
ActivateEssentials();
ChangeColor();
AssignButtons();
TurnDecider();
if (isServer)
SetName("Host Player - Local Player");
else
SetName("Client Player - Local Player");
}
#endregion
#region Commands
[Command]
public void CmdFire()
{
GameObject ball = Instantiate(Ball,activePlayer.shooter.position,Quaternion.identity) as GameObject;
ball.GetComponent<Rigidbody>().velocity = (obj.position - activePlayer.shooter.position).normalized * Power;
NetworkServer.Spawn(ball);
}
#endregion
#region Client Rpc
[ClientRpc]
public void RpcFire()
{
if (!isServer)
{
print("Called RPC Fire()");
}
}
#endregion
#region Other Functions
public void SetName(string name)
{
transform.name=name;
}
public void AssignTags()
{
transform.tag=targetTag;
player1.tag=targetTag;
player2.tag=targetTag;
player3.tag=targetTag;
}
public void AssignButtons()
{
b1.onClick.AddListener(player1.Jump);
b2.onClick.AddListener(player2.Jump);
b3.onClick.AddListener(player3.Jump);
}
public void ActivateEssentials()
{
playerNumber = 1;
myCamera.gameObject.SetActive(true);
myCanvas.gameObject.SetActive(true);
}
public void ChangeColor()
{
player1color.material.color = Color.green;
player2color.material.color = Color.green;
player3color.material.color = Color.green;
}
public void PlayerSelector()
{
if (playerNumber == 3)
playerNumber = 1;
else
playerNumber++;
TurnDecider();
}
public void TurnDecider()
{
switch (playerNumber)
{
case 1:
player1.isReady=false;
player2.isReady = true;
player3.isReady = false;
activePlayer=player2;
break;
case 2:
player1.isReady=false;
player2.isReady = false;
player3.isReady = true;
activePlayer=player3;
break;
case 3:
player1.isReady=true;
player2.isReady = false;
player3.isReady = false;
activePlayer=player1;
break;
}
}
#endregion
}