I am trying to make my gun rapid fire in my game. Every time I press start it throws me a Null Reference Exception here is my code. It was working fine in previous versions of unity but I decided to try the beta of 5.6 and I am now getting this error. The error is on line 26. Thanks :).
using UnityEngine;
using UnityEngine.Networking;
[RequireComponent(typeof(WeaponManager))]
public class PlayerShoot : NetworkBehaviour {
private const string PLAYER_TAG = "Player";
private PlayerWeapon currentWeapon;
[SerializeField]
private Camera cam;
[SerializeField]
private LayerMask mask;
private WeaponManager weaponManager;
void Start ()
{
if (cam == null) {
Debug.LogError ("PlayerShoot: No camera referenced!");
this.enabled = false;
}
weaponManager = GetComponent<WeaponManager> ();
}
void Update ()
{
currentWeapon = weaponManager.GetCurrentWeapon ();
if (currentWeapon.fireRate <= 0f) {
if (Input.GetButtonDown ("Fire1")) {
Shoot ();
}
} else {
if (Input.GetButtonDown ("Fire1")) {
InvokeRepeating ("Shoot", 0f, 1f / currentWeapon.fireRate);
} else if (Input.GetButtonUp ("Fire1")) {
CancelInvoke ("Shoot");
}
}
}
[Client]
void Shoot ()
{
RaycastHit _hit;
if (Physics.Raycast (cam.transform.position, cam.transform.forward, out _hit, currentWeapon.range, mask)) {
if (_hit.collider.tag == PLAYER_TAG) {
CmdPlayerHit (_hit.collider.name, currentWeapon.damage);
}
}
}
[Command]
void CmdPlayerHit (string _playerID, int _damage)
{
Debug.Log (_playerID + " has been shot.");
PlayerManager _player = GameController.GetPlayer (_playerID);
_player.RpcTakeDamage (_damage);
}
}