- Home /
Question by
1234filip · Jun 22, 2017 at 11:23 AM ·
networkinginstantiatenetwork instantiate
Network Manager NetworkServer.Spawn isn't working
Hey!
I'm making a MP FPS game. And my bullets are projectiles. I can't spawn them in on ther server side I get the NullRefernceException error.
using UnityEngine;
using UnityEngine.Networking;
public class PlayerShoot : NetworkBehaviour {
[SerializeField]
private Camera cam;
[SerializeField]
GameObject bullet;
[SerializeField]
Transform spawn;
[SerializeField]
private LayerMask mask;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown("Fire1"))
{
Shoot();
}
}
void Shoot()
{
RaycastHit hit;
if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, float.PositiveInfinity, mask))
{
Debug.Log(hit.point);
GameObject bulletClone = Instantiate(bullet, spawn.position, spawn.rotation);
bulletClone.transform.LookAt(hit.point);
bulletClone.transform.Rotate(new Vector3(90, 0, 0));
CmdFire(bulletClone);
}
}
[Command]
void CmdFire(GameObject bulletSpawn)
{
NetworkServer.Spawn(bulletSpawn);
}
}
Comment
Having pretty much the same problem. If I come up with anything I will be sure to come back with an answer.