- Home /
 
Raycast to determine certain game objects?
I want to use a raycast to determine different game objects. So far I have a script which uses a raycast. When it hits and object and the left mouse button is pressed it changes a boolean to true. In a separate script, it checks if the variable is true and enables some GUI. I want to know how to determine what object the raycast hits and change what GUI appear because of it.
Answer by clunk47 · Sep 06, 2013 at 11:18 PM
I'll answer your original question, about raycasting. The GUI topic should be a separate question. Here's a simple C# example on how to get the name of an object the ray is hitting. This uses a ray from mouse position from the main camera's perspective.
 using UnityEngine;
 using System.Collections;
 
 public class Example : MonoBehaviour
 {
     RaycastHit hit;
     Ray ray;
 
     void Update()
     {
         ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
         if(Physics.Raycast(ray, out hit))
         {
             Debug.Log(hit.collider.gameObject.name);
         }
     }
 }
 
              If this answer helped resolve your concern, please vote up (thumbs up) and accept (check mark) the answer.
Your answer
 
             Follow this Question
Related Questions
GUI label on raycast? 0 Answers
How to use raycast here? 2 Answers
Trouble targeting GUI with raycast 0 Answers
Knowing the position of the next GUI Image 1 Answer
GUI.button and onClick Terrain 2 Answers