Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

if statement of RaycastHit #31

Open
kushgupta42 opened this issue Apr 27, 2019 · 2 comments
Open

if statement of RaycastHit #31

kushgupta42 opened this issue Apr 27, 2019 · 2 comments

Comments

@kushgupta42
Copy link

kushgupta42 commented Apr 27, 2019

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
Copy link

I got the same error

@matthew1232
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants