Mirror Spawning prefab from Client with client authority enabled crash due to command parameters become null
My code in player prefab has a component script that contains the following for spawning a bullet.
It’s using ‘Mirror’. The ‘NetworkTransform’ has ClientAuthority
enabled.
void Update()
{
if (!isLocalPlayer)
{
return; // dont run if not the local player
}
// .... some code....
UpdateAction();
// ..... more code ....
}
void UpdateAction()
{
if (targetOther != null)
{
Shoot();
targetOther = null;
}
}
void Shoot()
{
print("Bullet in Shoot : " + bullet + "\n is null ? " + (bullet == null));
CmdShoot(bullet, transform.position + new Vector3(0, 2, 0), targetOther.gameObject);
}
[Command]
public void CmdShoot(GameObject bulletPrefab, Vector3 spawnPos, GameObject targetOther)
{
print("Bullet Prefab : " + bulletPrefab +"\n is null ? " + (bulletPrefab == null));
GameObject go = Instantiate(bulletPrefab);
print(go);
go.transform.position = spawnPos;
HomingAttack ha;
if (go.TryGetComponent<HomingAttack>(out ha))
{
ha._target = targetOther;
}
NetworkServer.Spawn(go);
}
The GameObject bullet
is a variable defined as public GameObject bullet;
in the script. I have assigned this variable a proper prefab which has a NetworkIdentity and a NetworkTransform. Also, the prefab is added to registered spawnable prefabs.
The problem is the code crashes when this part of code is run ( it runs when I right click on another player ) since it reports that the bullet
variable is null.
I have checked this with the print statements on line 26 and 33.
When run by the host-server player: line 26 prints that it has a valid value but line 33 prints that it is null.
How should I fix this?
Thank you.