- Home /
 
How to raycast hit empty space?
Hi Guys,
How do I trigger my raycast for hitting the empty space?
This part is where my raycast will hit the empty space & trigger the print("no object found") message but it didn't
 if (matchOne == null)
     {
         print("No object found!");
     }
  
   function Update () {
     if(Input.GetButtonDown("Fire1"))
     {
         var ray1 = Camera.main.ScreenPointToRay(Input.mousePosition);
         if (Physics.Raycast(ray1, hit, Mathf.Infinity))
         {
             if(!matchOne)
             {
                 revealCardOne();
             }
             
             else
             {
                 revealCardTwo();
             }
         }
         print("The mouse button has been clicked!");
     }
 }
   function revealCardOne()
 {
     matchOne = hit.transform.gameObject;
     tileName1 = matchOne.transform.parent.name;
     
     if (matchOne == null)
     {
         print("No object found!");
     }
     else
     {
         tName1 = tileName1.Split("_"[0]);
         print("Name: " + tName1);
     }
 
               }
Thanks!
First, find out what your raycast is actually hitting, if anything. This is NOT a suitable question since it's too specific and just clutters up the site, making it difficult for new users to find answers they need.
Answer by whydoidoit · Jun 24, 2012 at 12:28 AM
So it doesn't happen because you don't call revealCardOne() unless the ray cast hit something :)
Physics.RayCast returns true only if you click something...
You are looking for an else to go with that if(Physics.RayCast
    if(Physics.RayCast(...
    {
       ...
    }
    else
    {
          print("No object found");
    }
 
               EDIT Question title has changed:
You cannot get a hit on nothing with a ray cast - see above for how to tell that you have hit nothing, which is technically not a hit :)
His print command is outside the if block though, so it should print. It's more likely that his Raycast failed (unless he changed the code within the last 6 $$anonymous$$utes).
sorry guys! I realize my question need to correct, I changed it! I'm more concerned about the print command under the function revealCardOne.
it works when i hit an gameobject, but when I hit the empty scene, it supposed to print("No object found"), but it didn't.
@$$anonymous$$izuho - look again - the print he is after is in a function - that function is not called.
@moonLite - see my answer which is the solution your problem
Thanks $$anonymous$$ike! =) So in the end, it's still more like "If" raycast does not happen "Else" triggers "no object found"
FYI,
    if (Physics.Raycast(ray1, hit, $$anonymous$$athf.Infinity))
    {
          if(!matchOne)
          {
         revealCardOne();
         print(matchOne + "2");
          }
           else
          {
             revealCardTwo();
          }
     }
      // below is the part where to 
      //check raycast has been casted & casted on empty scene 
     else
        {
          print("No object found - new msg"); 
        }
                 Your answer
 
             Follow this Question
Related Questions
Unity's Equivalent of Console.Write(); 1 Answer
Unity Console not showing result 1 Answer
Raycast not reporting what it hit 1 Answer
Render Debug.Log as UI Text 2 Answers
[Resolved] Why unity print 0.08f + 0.02f as 0.0999999 instead of 0.1? 1 Answer