NULL REFERENCE DRVING ME INSANE!!! HELP!!!!
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);
}
}
Answer by KoenigX3 · Jan 14, 2017 at 09:13 AM
The currentWeapon variable has nothing assigned to it.
Maybe the GetCurrentWeapon() method could not initialize the variable. Check the WeaponManager class if the method can return a PlayerWeapon variable.
If you could not find the problem, you should include the class here.
In the Weapon$$anonymous$$anager class on start it is setting the current weapon to the primary weapon and the PlayerShoot class is using the GetCurrentWeapon method to return the current weapon and when I press play it seems to just be forgetting the current weapon.
Follow this Question
Related Questions
Failed to Copy Assembly-CSharp.dll on Script-Compilation 0 Answers
compiler error - playmode 0 Answers
Compiler error 2 Answers
Compiler probs Pls help 0 Answers
unity 5.6.1 run js but not c# 0 Answers