Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Yuuji_Sakakibara · May 03, 2012 at 10:57 AM · gameobjectinputmouse

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
9

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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
 




Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Drazion · May 16, 2015 at 12:10 PM 1
Share

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

avatar image hydrix · Jan 18, 2017 at 03:25 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges