Unity Raycasting Issue
Hello! (C#) I have in my game first person player, with an empty object on the top (Used as a point from where raycast is used).
I have this script on the point:
 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 
 public class ObjectDetection : MonoBehaviour {
 
 
     public float rayDist = 3;
     public Text pickUpText;
     
 
     void Update()
     {
 
 
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit;
 
 
         if (Physics.Raycast (ray, out hit, rayDist))
         {
             Debug.Log ("hit");
             
             if(hit.collider.gameObject.tag == "test1") 
             {
             pickUpText.text = "Press E to pickup BLAH"; 
                 isHit = true;
                 Debug.Log("hit");
                 //hit.collider.gameObject.GetComponent<MeshRenderer>().enabled = false;
                 
 
 
             }
             
 
 
             //Only For Debug
             if(showHit == true)
             {
             Debug.DrawLine(ray.origin, hit.point);
             Debug.DrawLine(hit.point + (Vector3.up  / 2),hit.point + (Vector3.down / 2),Color.blue);
             Debug.DrawLine(hit.point + (Vector3.left / 2),hit.point + (Vector3.right / 2),Color.red);
             }
 
         }
         else 
         pickUpText.text = "";
         Debug.Log("not hit");
         
 
 
     }
 
 
 }
 
               So when i play the game:
if the ray hits nothing -- the console prints "not hit"
if the ray hits an object with tag test1 -- the console prints "not hit", "hit", "not hit", "hit", "not hit", "hit".... and so on "
please tell me WHY? And how to enable the MeshRender when thy Ray leaves the object??
Thanks!
Answer by gjf · Jan 09, 2016 at 09:53 PM
it's because you're ALWAYS printing "not hit" - you need to enclose the lines following the else in braces. as it is right now, the else statement is just for pickUpText.text = "";
easy mistake to make ;)
Thanks! Now it works!! But the text blinks really fast. Why? And when I export the game to *.exe it's ok and not blinking.
Am I doing something wrong?
The complete Code:
 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 
 public class ObjectDetection : $$anonymous$$onoBehaviour {
 
 
     public float rayDist = 3;
     public bool showHit;
     public Text pickUpText;
     public bool isHit;
     public GameObject hitObject;
 
     void Start () {
 
 
     }
     
     void Update()
     {
 
 
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit;
 
 
         if (Physics.Raycast (ray, out hit, rayDist)) { //Creates a RAY
             
             
             if (hit.collider.gameObject.tag == "test1") {
                 pickUpText.text = "Press E to pickup BLAH"; 
                 isHit = true;
                 Debug.Log ("hit");
                 hit.collider.gameObject.GetComponent<$$anonymous$$eshRenderer>().enabled = false;
                 hitObject = hit.collider.gameObject; //Remember the object 
 
 
 
             }
 
 
             //hitObject.GetComponent<$$anonymous$$eshRenderer>().enabled = true;
 
 
 
 
             
             if (showHit == true) {
                     //Debug.DrawLine (ray.origin, hit.point);
                 Debug.DrawLine (hit.point + (Vector3.up / 2), hit.point + (Vector3.down / 2), Color.blue);
                 Debug.DrawLine (hit.point + (Vector3.left / 2), hit.point + (Vector3.right / 2), Color.red);
             }
 
         } 
         else 
         {
             pickUpText.text = "";
             Debug.Log ("not hit");
             isHit = false;
             hitObject.GetComponent<$$anonymous$$eshRenderer>().enabled = true;
         }
 
 
 
     }
 
 
 }
                 Your answer
 
             Follow this Question
Related Questions
Rayscast from the GameObject that is not the player does not work 0 Answers
Raycasting problem, colliders not being recognized? 1 Answer
An probably very easy question about Raycast system... 1 Answer
Accessing Raycast collider from another script 0 Answers
Raycast bug is making me go nnuts,Raycast bug is making me go nuts 0 Answers