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 xydroh · Apr 09, 2015 at 06:12 PM · mouseonmousedownunityui

OnMouseDown won't work on a simple button

Hello I tried starting up unity and add this simple script to a button but it just doesn't seem to work. I tried adding the script to buttons, panel's but the console just does nothing.

using UnityEngine; using System.Collections;

public class Click : MonoBehaviour { void OnMouseDown() { print ("click"); } }

this is a simple canvas with a panel and a button on It, there's just so little I can do wrong that I don't know why it won't work

Comment
Add comment · Show 2
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 supernat · Apr 09, 2015 at 06:14 PM 0
Share

Do other log entries work on your console? Do you have log entries turned on (the little balloon icon is enabled in the top right corner of the debug console?) Is there a collider on your button? http://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.On$$anonymous$$ouseDown.html

avatar image Pharaoh_ · Apr 09, 2015 at 06:15 PM 0
Share

Have you defined the respective event in the inspector? You add it with the "+" option, on the list.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by DoTA_KAMIKADzE · Apr 10, 2015 at 12:29 AM

You have 2 options to make it work:

1)Add BoxCollider2D to your Button and keep using MouseDown.

2)Use PointerDown:

 using UnityEngine;
 using UnityEngine.EventSystems;
 
 public class Click : MonoBehaviour, IPointerDownHandler
 {
     public void OnPointerDown(PointerEventData eventData)
     {
         print("click");
     }
 }

P.S. Though if you don't need a particular event for button you can do that as well:

     private Button buttRef;
     private void Awake()
     {
         buttRef = GetComponent<Button>();
     }
     private void Start()
     {
         if (buttRef != null) buttRef.onClick.AddListener(() => 
         {
             print("click");
         });
     }

Just FYI.

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
0

Answer by xydroh · Apr 10, 2015 at 04:34 PM

I've stopped trying to let the OnMouseDown work and I added a script to the button with a random method in it. I used the onClick thingie in the inspector and that's how I got to the method.

Thanks for helping me

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

RaycastHit only way to do OnMouse events for colliders in Mobile? 0 Answers

2D Game Mouse Problem 0 Answers

OnMouseDown for Right Mouse 2 Answers

Do something when there is no mouse click 1 Answer

Is it possible to use OnMouseDown(collision other) ??? 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