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 /
avatar image
0
Question by J0kerPanda · Jan 31, 2017 at 08:25 PM · 2dnot workingonmousedownmethod

OnMouseDown working only sometimes

I'm very sorry if this question was brought up like a thousand times but being quite new to Unity I didn't manage to find the solution for my problem nor to comprehend what the problem is.

Basically, in a 2d environment I have a group of GameObjects ( cells ) that are sprites with a collider and a script attached to them. I want to detect when the mouse is pressed on each individual cell by using OnMouseDown() method. However, it seems that it works only for some of the cells. And if I move the camera even a bit, some of the cells stop working as well.

So, I have three questions: 1. Am I going the right way? Are there simplier / safer ways? 2. How can I fix it? 3. What is causing the problem?

Code used for this:

 public class BasicObject : MonoBehaviour {
 
     void Start() {
 
         createMenu = false;
     }
 
     void OnMouseDown() {
 
         createMenu = true;
     }
 
     void OnMouseUp() {
 
         createMenu = false;
     }
 
     void OnGUI() {
 
         if ( createMenu ) {
 
             GUI.Box( new Rect( 100, 100, 20, 20 ), "Hallelujah" );
         }
     }
             
     [SerializeField] private string objectName;
     private bool createMenu;
 }
 

Picture of what cells are malfunctioning ( red don't work at all, blue don't work after I move camera )alt text

безымянныи.png (3.5 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by J0kerPanda · Feb 05, 2017 at 10:10 PM

Okay, so after another day or so I think I figured out approximately what was causing the problem and a workaround.

As you know, working in a 2D space and having a z-axis somewhere still leaves us in a 3D space even though we don't see it all the time. I used z-axis offsets for some sprite rendering stuff I needed and I believe that was causing the problem. Specifics, however, still stays unclear.

The different solution I found was quite simple - Raycasting from Physics 2D from the mouse position to the same point. It will get you the object(s) in the click position.

This is the exact code I'm using to do this.

 RaycastHit2D[] hits = Physics2D.RaycastAll( watchCamera_.ScreenToWorldPoint( Input.mousePosition ), new Vector3( 0, 0, 1 ) );

So, my advice would be using OnMouseDown only in simpliest cases because of the unclear nature of when the function works and when it doesn't.

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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

OnMouseDown Firing When I Miss 2DCollider 0 Answers

Character Not Moving RPG Game 0 Answers

Method doesn't work when called in other script 0 Answers

Ray2D from object to mousePos 1 Answer

Raycasting not working on my colliders 1 Answer


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