- Home /
How to get GameObject that is clicked by a mouse?
Hi, how can i get the GameObject that is clicked by a mouse by storing it in a variable? in example:
GameObject x;
now how do i send the Object i clicked into x? I can put
void OnMouseDown(){ Sendmessage("UpdateObject", this.gameObject); }
on all object, but it would be kind of a pain, is there any easier way to know what object is being clicked and how to point into that object? Sorry for the awful english
Answer by dannyskim · May 03, 2012 at 06:10 PM
Using a Raycast from your camera is the best option. The RayCastHit that is used in conjunction with a RayCast will return information about the collider that was hit, including the transform:
void Update()
{
if( Input.GetMouseButtonDown(0) )
{
Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition );
RaycastHit hit;
if( Physics.Raycast( ray, out hit, 100 ) )
{
Debug.Log( hit.transform.gameObject.name );
}
}
}
You will notice that I accessed the transform, then the gameObject. The gameObject I believe isn't returned with the RayCastHit, but the gameObject is an inherited member of the transform that is returned.
Answer by BurningKrome · Feb 12, 2015 at 12:16 PM
I realize this is an old post, but I wonder if there's a programmatic reason not to do it as follows:
Put OnMouseDown/Drag/up in the to-be-clicked object itself.
Put the actions to be done on clicked object either in the to-be-clicked object itself OR in GameControl.
I.e.
(GameControl)
public class GameControlScript : MonoBehaviour {
public GameObject clickedGameObject;
public void DoSomethingToClicked(GameObject clickedOn) {
clickedGameObject = clickedOn;
// Do things with clickedGameObject;
}// end DoSomethingToClicked
}//endclass
(Object)
public class ObjectScript : MonoBehaviour {
public GameControlScript control;
void Start () {
control = GameObject.Find("GameControl").GetComponent<GameControlScript>();
}// end start
void OnMouseUp() {
control.DoSomethingToClicked(this.transform.GameObject);
}//end OnMouseUp
The only reason I could think of to not do it at the GameObject level is that if you have a lot of objects you're adding a whole host of additional checks as Unity3d runs each frame. Additionally, if you need to change how that GameObject reacts to a click, you would need to change it in (possibly) multiple places. Just my 2 cents
one time I made a 2D game and put this on all the tiles. The overhead of having a GameObject was too high so I had to switch to "painting" on a giant Texture2D composed of 1,000,000 pixels. Then I had to switch from using On$$anonymous$$ouseDown to the other solution. Also the code previously had to be managed from the Tile class itself which was a bit harder to manage
Your answer
Follow this Question
Related Questions
Erratic mouse Input.GetAxis values on OS X 0 Answers
Switch Between Touch And Mouse Controls 0 Answers
Input.inputString not detecting mouse action? 1 Answer
Unity3D mouse events not working on Amazon Web Services EC2 instance in Play mode 0 Answers
Destroy object with more clicks depending on Scale? 0 Answers