- Home /
RacastHit object not set to an instance of an object
using UnityEngine; using System.Collections;
public class Player_Seeing : MonoBehaviour {
public GameObject player;
Ray sight;
Vector3 targetDir;
RaycastHit hit;
private float sightAngle;
private float maxAngle = 90;
void Start ()
{
}
void OnTriggerStay (Collider col)
{
if (col.tag == "Enemy")
{
sight = new Ray(transform.position, col.transform.position - transform.position);
targetDir = col.transform.position - transform.position;
sightAngle = Vector3.Angle(targetDir, transform.forward);
if (sightAngle <= maxAngle && Physics.Raycast(sight, out hit, 45) && hit.transform.tag == "Enemy")
{
hit.transform.gameObject.layer = 9;
print("see");
}
else
{
hit.transform.gameObject.layer = 9;
print("no see");
}
}
}
}
The game I am creating is a top down game where you can only see enemies in the direction you are looking. I have the script to look and move accordingly done but now I am working on the looking aspect. It shoots a ray cast to the enemy when they are in the trigger area (a sphere around the player) and checks the angle and if there is an object in the way. if it is in these parameters than the enemy is put on a layer the camera can see, if not it is moved to a layer the camera can not see. my problem is when I run my scene in unity it pauses and says object not set to an instance of an object. I understand this means that there is not object to referance so it can't run the code. If I un-pause manually the script works as it should, moving layers when i am / am not looking at the object. Why does it give me this error?
Here's the problem here :
if (sightAngle <= maxAngle && Physics.Raycast(sight, out hit, 45) && hit.transform.tag == "Enemy")
try debugging that out first for something like this:
if (Physics.Raycast(sight, out hit, 100))
Debug.Log(hit.collider.gameObject.name);
Ok I will try that tonight. That wasn't the line that was giving me the error, it was the code in the else statement @Paul$$anonymous$$evin
Answer by Dinosaurs · Dec 06, 2017 at 08:03 PM
The issue is this if/else:
if (sightAngle <= maxAngle && Physics.Raycast(sight, out hit, 45) && hit.transform.tag == "Enemy")
{
hit.transform.gameObject.layer = 9;
print("see");
}
else
{
hit.transform.gameObject.layer = 9;
print("no see");
}
if the angle check fails:
sightAngle <= maxAngle
then the RaycastHit 'hit' is never assigned, so it's still null when you try to assign its layer. You're also assigning the same layer regardless of whether they're seen or not...
What you probably want it something more like:
else
{
col.gameObject.layer = 9;
print("no see");
}
which references the collider, instead of the (null) raycast hit.
Thanks so much, i feel stupid for not realizing that. I should have read more about how ray casts work and where it gets its hit information. Cheers
Your answer
Follow this Question
Related Questions
Simple AddForce Scirpt help 1 Answer
overloaded Raycast 0 Answers
NullReference when checking tag via Raycast. How to solve this? 1 Answer
RaycastHit CS0165 Error 1 Answer
My raycast randomly stops working. 0 Answers