How To Perform A Mouse Click On Game Object

Hello,

I would like the ability to click on an object in the game play.

Eg: user clicks on cube in the game, a dialog window then pops up, or an event is triggered.

I have no person controls or anything, just a camera.

--- Snippet of Scrrpt -----

function Update () 
{
    if(Input.GetMouseButtonDown (0) && Clicked == false)
    {

This works, but I can click anywhere on the screen....

You can use the OnMouseDown function in a script component attached to your cube, as long as it has a collider: http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.OnMouseDown.html

try this

if ( Input.GetMouseButtonDown(0)){
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var select = GameObject.FindWithTag("select").transform;
if (Physics.Raycast (ray, hit, 100.0)){
select.tag = "none";
hit.collider.transform.tag = "select";
}
}

I was having some issues with Uriel’s code, so I tweaked it a little to simplify it. Here is the solution that worked for me.

I created a cube which had a “Box Collider” on it by default and renamed it to “targetArea”. I then added a script to it with the following update statement…

function Update ()
{
    if ( Input.GetMouseButtonDown(0))
    {
        var hit : RaycastHit;
        var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
        
        if (Physics.Raycast (ray, hit, 100.0))
        {  
            Destroy(GameObject.Find("targetArea"));
        }
    }
}

Look at our tutorial! - YouTube

Void OnMouseClick()
{
function
}