Why is negative operand not working?

i need the raycast to detect when it hits an object called HEX

this works ok but i need too detect if the ray is not hitting HEX

i am using the negation operand but it is not working

if (hit.transform.gameObject.name != "HEX"){

                Debug.Log("MISS");
            }else{
Debug.Log"HIT")
}

The code would work. Try printing the name and see what it actually is that it hits.

1 Like

There’s several ways to interpret this:

  • test if I’m hitting nothing

  • test to make sure that I am hitting things, but make sure they’re not named HEX

Your code seems to be doing the latter.

But as Dragon says, print stuff out. You know this already:

You must find a way to get the information you need in order to reason about what the problem is.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as Debug.Log("Problem!",this);

If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://discussions.unity.com/t/700551 or this answer for Android: https://discussions.unity.com/t/699654

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3

When in doubt, print it out!™

Note: the print() function is an alias for Debug.Log() provided by the MonoBehaviour class.

1 Like

see i thought the code should work but for some reason it just isnt, it still shows the debug as hit when it hits other objects, i need it to detect when it is not hitting anything like firing at the sky, i thought that would be null but that also does not register,

how would i detect when the ray is not hitting the HEX, say i had a gun and it could only fire if the ray was touching HEX and nothing else, how would i code it to only detect HEX, I THOUGHT THAT WOULD BE ==“HEX” but that also still detects when i hit other objects, do you think this is a bug, i am using an older unity 2018 version as i can not upgrade due to some assets in my project that wont upgrade

Walk through your code and what you expect values to be when:

  • you hit absolutely nothing (hit.transform is likely null, right?)

  • you hit something:
    —> it is called HEX
    —> it is NOT called HEX

You’ll see you’re not handling all cases correctly if you take it one step at a time.

1 Like

so how do i detect if the ray hits nothing at all? not another object but absolutely nothing

i have tried
if (hit.collider == null)

but it does not work for some reason, it says it should in the unity docs?

hit == null

The .collider only exists if the hit happened.

just found this

if (!hit)

will give it a go later see if it works

god of course, why did i use hit.collider if i want to detect nothing hit, what a fool i am, too much coding time brain corroded thank you

Hey, the brain is a delicate organ sometimes needs time to process things.
Balance the yin and yang. Urgency creates frustration. Make sure you are well fed.

It can be helpful to explain it to your dog or your houseplant.

“This line here checks the name if I hit nothing…”

or,

“If I hit nothing then I check its name…”

And it will sound wrong.

You can see me explaining a bug to my dog in my avatar picture.

Wow, there seems to be a lot misinformation around here ^^. First of all, you should clear up if you talk about Physics, or Physics2D. Those are two seperate physics systems which work different. The normal 3d “Physics” Raycast method returns a bool value indicating if it hit something or not. So accessing anything from the hit structure when the method returns false should already be avoided.

No matter which physics system you’re using, the RaycastHit and RaycastHit2D types are structs and therefore can not be compared to null.

This does only work with the RaycastHit2D struct as it implements an implicit conversion operator to bool which internally checks if hit.collider is null or not. Physics2D.Raycast works different as it does not return a bool, but directly returns a RaycastHit2D struct. Checking the collider property is the only way to know if you hit something or not, though for 2d they implemented this conversion operator as a shortcut.

Well, this should actually work in both cases (we still don’t know if you use 2d or 3d physics. Though if if(!hit) works, you’re probably using Physics2D?) as the collider property of both structs simply use Object.FindObjectFromInstanceID internally by passing the internal colliderID to that method. So the only two possibilities are: the id is invalid and you get null as a result, or if the id is valid, it would return the collider instance.

This:

won’t work as hit.transform would return null if you didn’t hit anything. So trying to get the gameobject from a null reference would give you a null reference exception.

So it seems a lot wrong conclusions has been drawn here.

3 Likes

You’re quite right bunno

if ray out hit

else // I am hit null

t

tried it but get error:
error CS0019: Operator ==' cannot be applied to operands of type UnityEngine.RaycastHit’ and `null’

why is this so dificult, all i want to do is detect when the ray hits the object named HEX, i done that

but then i want to do something else when the ray is not hitting HEX, so in other words when the ray is either touching or not touchng anything but HEX, tried so many ways but none seem to work, why can i not use the ! negation operator for this so it means anything other than HEX?

using 3d not 2d

             RaycastHit2D hit = Physics2D.Raycast(transform.position, -Vector2.up);
                if (hit == false)
                {
               
                }

Try this one

looks like Raycast2D’s hit is a bool. Raycast3D hit is not a bool and can be checked for null or else.

tried t, just gives errors

Sorry can’t help

this is getting ridiculous, how can this be impossible?

all i need is

if ray hit HEX
do this
if ray hits anything else or nothing
do this

but it seems impossible to do

can someone from unity explain why this does not work

if(hit.transform.gameObject.name != “HEX”){

Debug.Log(“HIT”);
}else{
Debug.Log(“MISS”);

MISS is never called even when the ray is NOT touching the object named HEX

how can i detect when the raycast is NOT touching the gameobject named HEX or anything else? is this really impossible too do in unity?

Maybe read the post by @Bunny83 which explained some crucial details you seem to have ignored.

You also need to make sure the objects you’re hitting have colliders.

2 Likes