Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Nov 15, 2018 at 05:31 AM by Bunny83 for the following reason:

The question is answered, right answer was accepted

avatar image
2
Question by Aydan · Feb 06, 2012 at 01:04 AM · gameobjectraycastraycasthitnull

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image aspscratch · Jun 09, 2020 at 03:49 PM 0
Share

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?

3 Replies

  • Sort: 
avatar image
10
Best Answer

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;
         }
     }
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Aydan · Feb 06, 2012 at 01:43 AM 0
Share

awesome :D thanks

avatar image Bunny83 · Feb 06, 2012 at 03:29 AM 0
Share

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.

avatar image
5

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
-2

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!
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Nov 15, 2018 at 05:31 AM 1
Share

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

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Ray Casting - Trigger function 1 Answer

Raycast and RaycastHit failure 2 Answers

Play ParticleSystem on RayCastHit Object 1 Answer

Using a ray to make drop script 1 Answer

See what object I hit in raycast 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges