Hey!
Bottom Line Up Front:
Keep getting a Null Reference Expection:
NullReferenceException: Object reference not set to an instance of an object
Movement.Update () (at Assets/Scripts/Player/Movement.cs:96)
I have no idea what object I’m missing a reference for. The line tags 96:
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
Has no objects attached to it? Is there something I’m missing? I’m just trying to get my Player to focus on something I left click.
I have an interactableLayer and I have an interactable script which I’ve attached to a box. I will include it in the code after my movement code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class Movement : MonoBehaviour
{
NavMeshAgent agent;
public LayerMask groundLayer;
public LayerMask enemyLayer;
public LayerMask interactableLayer;
public float rotateSpeedMovement = 0.01f;
float rotateVelocity;
public Interactable focus;
Camera cam; // Reference to our camera
// Start is called before the first frame update
void Start()
{
agent = gameObject.GetComponent<NavMeshAgent>();
}
// Update is called once per frame
void Update()
{
//if (EventSystem.current.IsPointerOverGameObject())
//return;
//When Right Button Pushed
if (Input.GetMouseButtonDown(1))
{
RaycastHit hit;
//Check if the raycast shot hits something that uses the nav mesh system.
if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, Mathf.Infinity, groundLayer))
{
//MOVEMENT
//Have the player move to the raycast/hit point
agent.SetDestination(hit.point);
//Rotation
Quaternion rotationToLookAt = Quaternion.LookRotation(hit.point - transform.position);
float rotationY = Mathf.SmoothDampAngle(transform.eulerAngles.y,
rotationToLookAt.eulerAngles.y,
ref rotateVelocity,
rotateSpeedMovement * (Time.deltaTime * 5));
transform.eulerAngles = new Vector3(0, rotationY, 0);
}
//This should be if you target an enemy
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, Mathf.Infinity,enemyLayer))
{
//MOVEMENT
//Have the player move to the raycast/hit point
agent.SetDestination(hit.point);
//Rotation
Quaternion rotationToLookAt = Quaternion.LookRotation(hit.point - transform.position);
float rotationY = Mathf.SmoothDampAngle(transform.eulerAngles.y,
rotationToLookAt.eulerAngles.y,
ref rotateVelocity,
rotateSpeedMovement * (Time.deltaTime * 5));
transform.eulerAngles = new Vector3(0, rotationY, 0);
}
//This is for InteractableObjects
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, Mathf.Infinity, interactableLayer))
{
//MOVEMENT
//Have the player move to the raycast/hit point
agent.SetDestination(hit.point);
//Rotation
Quaternion rotationToLookAt = Quaternion.LookRotation(hit.point - transform.position);
float rotationY = Mathf.SmoothDampAngle(transform.eulerAngles.y,
rotationToLookAt.eulerAngles.y,
ref rotateVelocity,
rotateSpeedMovement * (Time.deltaTime * 5));
transform.eulerAngles = new Vector3(0, rotationY, 0);
}
}
//When left button is pushed
if (Input.GetMouseButtonDown(0))
{
// Shoot out a ray
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
// If we hit
if (Physics.Raycast(ray, out hit, Mathf.Infinity, interactableLayer))
{
SetFocus(hit.collider.GetComponent<Interactable>());
}
}
//Escape Key being pushed
if (Input.GetKey(KeyCode.Escape))
{
RemoveFocus();
}
}
//Method for focusing item/player
void SetFocus (Interactable newFocus)
{
focus = newFocus;
}
//Method for removing focus
void RemoveFocus()
{
focus = null;
}
}
My Interactable code:
using UnityEngine;
public class Interactable : MonoBehaviour
{
public float radius = 3f;
void OnDrawGizmosSelected()
{
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(transform.position, radius);
}
}
Any help provided I would be greatful. Thank you!