Open kushgupta42 opened 5 years ago
using UnityEngine; using UnityEngine.Networking; using System.Collections;
public class PlayerShoot : NetworkBehaviour { private const string PLAYER_TAG = "Player"; private PlayerWeapon weapon; [SerializeField] private Camera cam; [SerializeField] private LayerMask mask;
void Start () { if (cam == null) { Debug.LogError("PlayerShoot: No camera referenced!"); this.enabled = false; } //weaponManager = GetComponent<WeaponManager>(); } void Update() { if(Input.GetButtonDown("Fire1")) { Shoot(); } } [Client] void Shoot() { RaycastHit _hit; // below line gives me error if (Physics.Raycast(cam.transform.position, cam.transform.forward, out _hit, weapon.range, mask) ) { //Debug.Log("we hit "+_hit.collider.name); if(_hit.collider.tag == PLAYER_TAG) { CmdPlayerShot(_hit.collider.name,weapon.damage); } } } [Command] void CmdPlayerShot (string _playerID, int _damage) { Debug.Log(_playerID + " has been shot."); Player _player = GameManager.GetPlayer(_playerID); _player.RpcTakeDamage(_damage); }
} // error : Object reference not set to an instance of an object
I got the same error
Drag the "Player Weapon" script on the player prefab into the "Weapon" variable for the "Player Shoot" script. The issue is because the weapon isn't set.
using UnityEngine; using UnityEngine.Networking; using System.Collections;
public class PlayerShoot : NetworkBehaviour { private const string PLAYER_TAG = "Player"; private PlayerWeapon weapon; [SerializeField] private Camera cam; [SerializeField] private LayerMask mask;
} // error : Object reference not set to an instance of an object