- Home /
The question is answered, right answer was accepted
How do I ask if RaycastHit returns null?
I am trying to ask if my RaycastHit didnt hit anything. Heres my script: var curSelection : GameObject;
var object : GameObject;
function Update () {
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if(Input.GetMouseButtonDown(0)){
if (Physics.Raycast (ray, hit, 100)){
if(hit == null){
object = null;
}
}
}
}
whats wrong with it?
wouldn't if(hit.transform.gameObject == null) {
}
this be easier, to simple check if the there is a game object saved in the ray-cast-hit?
Answer by karl_ · Feb 06, 2012 at 01:38 AM
You're already testing if the Raycast hit something with the if(Pysics.Raycast) statement.
How it should look:
var curSelection : GameObject;
var object : GameObject;
function Update () {
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if(Input.GetMouseButtonDown(0)){
if (Physics.Raycast (ray, hit, 100)){
// if not null, do stuff.
}
else // If Raycast did not return anything
{
object = null;
}
}
}
Just beside the fact that when Raycast returns true, hit will always contain reliable data (like @Wolfie stated), the actual problem was that RaycastHit is a struct (a value type) and therefore can't be null so if you compare a value type with null it will always fail.
Answer by Wolfie · Feb 06, 2012 at 01:44 AM
The problem is that by saying;
if (Physics.Raycast (ray, hit, 100)){
etc. etc...
you are basically asking "If the raycast hit something, see if the raycast didn't hit anything." I expect it could be fixed by simply using 'else'.
if(Input.GetMouseButtonDown(0)){
if (Physics.Raycast (ray, hit, 100)){
// Do stuff for rays that aren't null.
}
else {
// Do stuff for rays that are null.
}
}
Or perhaps;
if (!Physics.Raycast (ray, hit, 100)){
...
Well, I think those make sense anyway. I'm tired and haven't tested this, so do bear in mind that I may be inadvertently spouting a load of twaddle.
Edit - Dammit, beaten to it! At least we both gave the same answer though. :D
Answer by Arthur_Kenichi · Nov 15, 2018 at 03:21 AM
You can test the normal, too, I think. If it's Vector3.zero, the Raycast didn't hit anything. (C#:)
Physics.Raycast(Ray,out Hit,maxRayDistance,LayerMask.GetMask(Mask));
someObject.Hit=Hit;
// [... some coding later]
if(someObject.Hit.normal!=Vector3.zero){
// The Raycast hit something!
}
No, this is a misleading answer. When the raycast did not hit anything Raycast will return false. The hit structure should only be read when the Raycast method returns true. Yes, the normal might be Vector3.zero when the struct isn't filled with data, however that does not necessarily mean that you didn't hit anything.
Also the question was already answered almost 7 years ago.
Follow this Question
Related Questions
Using a ray to make drop script 1 Answer
See what object I hit in raycast 0 Answers
Grabbing MonoBehavior object from collision 1 Answer
Some GameObjects can't touch !!! 0 Answers
Raycast Based on GameObject Tag 0 Answers