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
1
Question by Shujee · Jul 27, 2017 at 05:29 AM · 2drigidbody2dmouseclickevent triggering

Hard time getting mouse click to work

Using Unity 2017.1.

I just can't get it to work; even with the simplest scenes. Here is what I did:

  1. Create a new 2D project.

  2. Add a sprite to the project and drag it to Hierarchy to create a GameObject.

  3. Add RigidBody2D component to this GameObject (not doing this step doesn't make a difference though).

Now I want it to do something on MouseDown. I tried the following two ways:

  1. Add a script to this object. Add the following function to it:

      void OnMouseDown()
      {    
      }
    
    

Place a breakpoint and run the project. Click on the GameObject. No mouse down.

  1. Add a new EventTrigger component. Add the following method to the script:

      public void Strike(BaseEventData d)
      {
      }
    
    

Link this function to the EventTrigger component using Pointer Down event. Place a breakpoint on the function and run. Click on the GameObject. Still no mouse down.

Can anyone help me figure out what's wrong here?

Edit

Here is the complete script:

 using UnityEngine;
 using UnityEngine.EventSystems;
 
 public class BoardController : MonoBehaviour
 {
   void OnMouseDown()                        //breakpoint on this line
   {
   }

   public void Strike(BaseEventData d)       //breakpoint on this line
   {
   }
 }
Comment
Add comment · Show 3
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 SteenPetersen · Jul 27, 2017 at 05:58 AM 0
Share

Hi @Shujee can you post the entire script you're attaching to the sprite?

avatar image Shujee SteenPetersen · Jul 27, 2017 at 06:03 AM 0
Share

Updated my question.

avatar image SteenPetersen SteenPetersen · Jul 27, 2017 at 06:40 AM 0
Share

Im guessing your trying to get an event from the user pressing on the board?

I'd use Debug for this btw.

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by dakshesh1010 · Jul 27, 2017 at 06:46 AM

Do you have any type of collider on the object. OnMouseDown() doesn't work without a collider.

Comment
Add comment · Show 5 · 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 Shujee · Jul 27, 2017 at 06:48 AM 0
Share

O$$anonymous$$G. That works. Is what you are saying true for EventTrigger too?

avatar image Shujee · Jul 27, 2017 at 06:52 AM 0
Share

This presents another problem though. I'm now able to click on the Board, but the pieces now get off the board as soon as the game starts, because of the collider.

avatar image SteenPetersen Shujee · Jul 27, 2017 at 06:52 AM 0
Share

$$anonymous$$ake it a trigger

avatar image Shujee SteenPetersen · Jul 27, 2017 at 06:57 AM 0
Share

Super. Thanks for the input.

Show more comments
avatar image
1

Answer by SteenPetersen · Jul 27, 2017 at 06:48 AM

I made an exact copy of the project and it works in mine if you add a collider, so I guess it must be an absent collider.

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

125 People are following this question.

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

Related Questions

Using child colliders with rigidbodies/joints in 2D 0 Answers

Drag and throw Rigidbody2d (Angry Birds style) 0 Answers

Unity 2D - Rigidbody2D Velocity sudden freeze 0 Answers

2D character movement is jittery (top-down) 1 Answer

Movement Relative to Rotated Parent 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