Brackeys / MultiplayerFPS-Tutorial

A video series on making a multiplayer first-person shooter in Unity.
The Unlicense
654 stars 287 forks source link

if statement of RaycastHit #31

Open kushgupta42 opened 5 years ago

kushgupta42 commented 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

HitarthGandhi commented 4 years ago

I got the same error

matthew1232 commented 4 years ago

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.