# Raycast Object Selection

**URL:** <https://discussions.unity.com/t/raycast-object-selection/88330>\
**Category:** Questions & Answers\
**Created:** [November 30, 2013, 2:16am UTC](https://discussions.unity.com/t/raycast-object-selection/88330 "2013-11-30T02:16:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Carbongrip](https://avatars.discourse-cdn.com/v4/letter/c/d07c76/32.png) [@Carbongrip](https://discussions.unity.com/u/Carbongrip)\
**Post date:** [November 30, 2013, 2:16am UTC](https://discussions.unity.com/t/raycast-object-selection/88330/1 "2013-11-30T02:16:43Z")

</div>

What’s wrong here? I have this in the update function and I debuted the thing… it see’s the click but never does the if it even when it should be hitting…

```
if (Input.GetMouseButtonDown(0))// when button clicked...
       	{ 
         	gui = "no";
         	RaycastHit hit; // cast a ray from mouse pointer:
         	Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         	// if enemy hit...
         	if (Physics.Raycast(ray, out hit))
         	{
         	   Debug.Log("selecting");
               DeselectPlanet(); // deselect previous target (if any)...
               selectedPlanet = hit.transform; // set the new one...
               planetselect(); // and select it
         	}
        }

```

I have this working in another script only thing I can think is it has to do with the fact that for this one the camera is in a empty object so I can move the camera. Also maybe since the camera is angled?

---

<div class="post-metadata">

**Author:** ![belvita](https://avatars.discourse-cdn.com/v4/letter/b/aca169/32.png) [@belvita](https://discussions.unity.com/u/belvita)\
**Post date:** [November 30, 2013, 5:05am UTC](https://discussions.unity.com/t/raycast-object-selection/88330/3 "2013-11-30T05:05:24Z")

</div>

Can u try like this: inside update function

if (Input.GetMouseButtonDown(0))

```
    {

```

// when button clicked…

```
        gui = "no";
    }
        RaycastHit hit; // cast a ray from mouse pointer:

        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        // if enemy hit...

        if (Physics.Raycast(ray, out hit))

        {

           Debug.Log("selecting");

           DeselectPlanet(); // deselect previous target (if any)...
           selectedPlanet = hit.transform; // set the new one...

           planetselect(); // and select it
        }

```

---

<div class="post-metadata">

**Author:** ![Tomer-Barkan](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/tomer-barkan/32/395756_2.png) [@Tomer-Barkan](https://discussions.unity.com/u/Tomer-Barkan)\
**Post date:** [November 30, 2013, 5:46am UTC](https://discussions.unity.com/t/raycast-object-selection/88330/4 "2013-11-30T05:46:01Z")

</div>

First, do you have just the one camera? If you have two cameras, you might want to use a reference to the camera inside your script instead of Camera.main (in general you might want to use it for performance benefits).

Second, I can recommend a perhaps simpler way to handle clicks on items: [OnMouseUpAsButton](http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnMouseUpAsButton.html). This method is invoked in any `MonoBehaviour` attached to an object that is clicked - meaning button is pressed and released on top of the object (as long as the object has a collider). You also have `OnMouseDown` to handle button pressed (without releasing), `OnMouseDrag` to handle holding the button down, `OnMouseUp`, etc.

Now, I would create a simple script that detects clicks on an object, and calls a method on your HUD script:

public class MapHud : MonoBehaviour {  
// all your code here

```
// handle clicks on objects
public void ObjectClicked(ClickableObject obj) {
    Debug.Log("selecting");
    DeselectPlanet(); // deselect previous target (if any)...
    selectedPlanet = obj.transform; // set the new one...
    planetselect(); // and select it
}

```

}

public class ClickableObject : MonoBehaviour {  
private MapHud hud;

```
public void Start() {
    hud = Object.FindObjectOfType(typeof(MapHud)) as MapHud;
}

public void OnMouseUpAsButton() {
    hud.ObjectClicked(this);
}

```

}

Then simply attach the ClickableObject script to any object you want selectable. Remember to make sure they all have colliders. In this case it doesn’t matter how many cameras you have.

---

<div class="post-metadata">

**Author:** ![golemsmk](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/golemsmk/32/932220_2.png) [@golemsmk](https://discussions.unity.com/u/golemsmk)\
**Post date:** [November 30, 2013, 4:56am UTC](https://discussions.unity.com/t/raycast-object-selection/88330/2 "2013-11-30T04:56:41Z")

</div>

I think your script is OK, it must be a problem with colliders. Adding a suitable collider to the object will solve your problem. Remember, you can only use the 3D collider, not 2D collider, these 2D colliders won’t work. Unity will fix this bug soon. I see someone has reported this to Unity.  
If this is the right answer, please stick it for other people don’t have to look around for the answer.
