Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by eymen34 · Mar 21, 2016 at 01:27 AM · cameramousemouseclick

CATCH MOUSE CLICK EVENT ON OBJECTS

Hi all,

First of all, as usual, im new to C# and Unity and trying to learn (this is the 2nd day :) ). I have googled and tried to use the codes but i wasnt able to successfully catch a mouse click on an object. Please help me with that. Here's it why i have and what i did.

I have a c# script where i create a list of objects in void start(),

  void Start() {
 
         for (int i = 0; i < numberofwords; i++)
         {      
            
             GameObject go = new GameObject();
             Transform tr = go.transform;
             tr = Instantiate(word, new Vector3(10-((1+ i*3)), 2, 0), Quaternion.identity) as Transform; 
             words.Add(tr);
         }
 
     }

then in update i check if these are still visible and set their position and velocity as i need but this part is not relevant.

I have attached this script to MainCamera.

this part is working as i expect, but im not sure if im doing something fundamentally wrong.

Now, im trying to catch the click with following code in Update func;

        if (Input.GetMouseButtonDown(0))
         {
           
             RaycastHit hit;
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            
 
             if (Physics.Raycast(ray, out hit,1000f))
             {
                 Debug.Log("Hit at : " + hit.point);
             }

} and i can catch the click and get its position but im not able to click on "Objects". I tried a few lines of code instead of Physics.Raycast but didnt work either.

Finally , I have box collider2d and rigidbody2d defined on my prefab.

Thanks in advance,

PS : Now i have added another object with Box Collider (Not 2d), and i was able to catch the click. So it seems i cant do it for 2d collider.

PS : Moderator , i found the solution, you can just reject this one :) When i tried to find something named RaycastHit2D and Ray2d i was able to find the following solution which works :

 Vector2 worldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             RaycastHit2D hit = Physics2D.Raycast(worldPoint, Vector2.zero);
             if (hit.collider != null)
             {
                 Debug.Log(hit.collider.name);
             }
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

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Why won't my cursor show up?? 1 Answer

Rotate towards mouse pointer 1 Answer

Mouse Events with Render Texture 0 Answers

camera and pointer help (camera look at pointer using (x,y) ?) 1 Answer

Detect mouse inputs when the scene is changing to another scene. 2 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