Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 zx1230516 · Dec 12, 2019 at 04:17 PM · scripting problemuiunity 2dui image

OnPointerClick in child's script is not triggered when there exists a OnPointerDown in parent's script

Hi, I have learned Unity for a few days, but I'm in trouble with the relationship between child and parent objects.
I have an UI structure in Hierarchy like this: alt text

Canvas' script

 public class CanvasEvent : MonoBehaviour, IPointerDownHandler
     {
         public void OnPointerDown(PointerEventData eventData)
         {
             print(name);
         }
     }

Back's script

 public class BackEvent : MonoBehaviour, IPointerClickHandler, IPointerDownHandler
     {
         public void OnPointerClick(PointerEventData eventData)
         {
             SceneManager.LoadScene("Menu");
         }
 
         public void OnPointerDown(PointerEventData eventData)
         {
             //OnPointerClick does not work without implement this.
         }
     }

There was not OnPointerDown event in Back's script in the beginning, and I want to load another scene by clicking Back image.
However, it doesn't work when I click it. It just triggered Canvas' OnPointerDown and print the name "Canvas" and nothing else happened.
I found it works if I delete Canvas' OnPointerDown or implement it in Back's script, but I don't know why I have to do this to make it work.


So my question is:
Why can't I trigger Back's click event without implement IOnPointerDownHandler?
Did it inherited parent's event? Or it's event just bubbled up to parent like javascript?

struct.jpg (7.2 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

2 Replies

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

Answer by zx1230516 · Dec 14, 2019 at 12:51 PM

After trying many times, I think here is the most possible answer:
The execution order is OnPointerDown -> OnPointerUp -> OnPointerClick. If an event is not implemented in child's script, it will bubble up until it find a implemented one in parent's script or reach the root and return the pointer behavior to the chlid.
PointerClick event needs a pointer "Down" and a pointer "Up" behavior, but my "Down" behavior has bubbled up to the parent object and has being captured, so even if the "Up" behavior is executed, there is no "Down" behavior can be combined to a click event in child's script.


Based on this guess, the solution is what I asked in the beginning:
1. Implementing an OnPointerDown event in child's script so that it can prevent pointer "Down" behavior from propagating to parent's script.
2. Deleting the OnPointerDown in the parent's script so that the pointer "Down" behavior won't be captured by any other parents' script.

Comment
Add comment · Show 1 · 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 F4t1h · Jun 12, 2021 at 09:47 AM 0
Share

I cant imagine how many hours you saved me with this one. Pretty hard to find answers on specific stuff like that. Thanks alot!

avatar image
0

Answer by Miguelfanclub · Mar 29 at 04:39 PM

OnPointerDown and OnPointerClick functions must be implemented (can be empty) to ensure functions to be called.

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

271 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 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 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

Anchoring a UI image to the top of another UI image. 1 Answer

How to match UI Image size to UI Text size when using ContentSizeFitter on UI Text? 1 Answer

Sprite shrinks when selected? 1 Answer

UI Image.Color Not Changing the GameObject In-Game 1 Answer

Raycast Not Working properly 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