Detect the Game Object was clicked?

I created a script call Dialog, when mouse click on the object display variable set to true.

void OnMouseUpAsButton(){
     display = true;
}
void OnGUI(){
	if(display)
		this.currentGUIMethod();
}

I created a prefab and import an 3d objects from internet into my project, I drag the 3d objects into prefab and drag the script into this prefab and add it into Scene. I want the object clicked the display will set to true to display a box. But the object since can’t detecth mouse click

if I add in this

	void Update () {
	if(Input.GetMouseButtonDown(1))
		display = true;
}

the box appear whenever I clicked on the game screen, which I only want the box appear when the object was clicked.

	GameObject GetClickedGameObject()
{
	// Builds a ray from camera point of view to the mouse position
	Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
	RaycastHit hit;
	// Casts the ray and get the first game object hit
	if (Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask))
		return hit.transform.gameObject;
	else
		return null;
}

// Update is called once per frame
void Update () {
	GameObject clickedGmObj = null;
	if(Input.GetMouseButtonDown(0)){
		clickedGmObj = GetClickedGameObject();
		if(clickedGmObj != null)
			Debug.Log(clickedGmObj.name);
			//display = true;
	}
}

This 2 method is under Dialog Script, I try to print out whether the object detected or not.
Untitled — Postimages ( This is the screen shot of the object inspector I imported from internet)
Untitled2 — Postimages ( I created a prefab in project tab and drag it to the Scene, attached the script on it)

Input.GetMouseButtonDown returns true whenever the mouse button is down, regardless of what is under the mouse pointer.

You can raycast from the mouse screen position and see if it hits the object in question. See get mouse position on object - Unity Answers for information about how to get the object under the mouse cursor.

Hope this helps :slight_smile: