So I am currently working on making a game, and im trying to get the raycasting to work. I have a private field, witch has been serialized, made to set up the camera from which the ray will be cast. Every time I try to shoot however, it pops up an error:
NullReferenceException: Object reference not set to an instance of an object
playerShoot.shoot () (at Assets/Scripts/playerShoot.cs:33)
playerShoot.Update () (at Assets/Scripts/playerShoot.cs:24)
I have tried making the variables public, but the error is still there. Here is the code as well:
using UnityEngine;
using UnityEngine.Networking;
public class playerShoot : NetworkBehaviour {
public playerWeapon weapon;
[SerializeField]
private Camera cam;
[SerializeField]
private LayerMask mask;
void Start()
{
if (cam == null)
{
Debug.LogError("No camera Assigned");
enabled = false;
}
}
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
shoot();
}
}
[Client]
void shoot()
{
RaycastHit _hit;
if(Physics.Raycast(cam.transform.position, cam.transform.forward, out _hit, weapon.range, mask))
{
if (_hit.collider.tag == "Player")
{
CmdPlayerShot(_hit.collider.name);
}
}
}
[Command]
void CmdPlayerShot(string _ID)
{
Debug.Log(_ID + "has been shot");
}
}
If anyone can help me figure out how to make it so that i can assign my variables in the inspector like im doing now, preferably without the variables being public, then that would be amazing.
Note that I have tried to ignore the warnings but that does not work.
Note 2: The error lines are a little off as I had edited it after the error to get the original code, but the error is still the same one.