• 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 armandleggames · Mar 10, 2015 at 02:23 PM · mouseclick

Detect children OnMouseDown

I have a object that consists of 3 different game objects, and its all under one empty object. I want to be able to detect OnMouseDown on all of the children through one script.

Comment
Add comment · Show 1
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 AlwaysSunny · Mar 10, 2015 at 12:53 AM 0
Share

Have a script on each child that responds to On$$anonymous$$ouseDown events by calling a public method the parent's script.

I do not believe another "catch events on my children" approach exists.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by ETGgames · Mar 31, 2020 at 07:37 PM

Couldn't you just add a static rigidbody to the parent? That way it combines all of it's child colliders, and OnMouseDown() and the like will 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
avatar image
1

Answer by Pendantic · Mar 10, 2015 at 02:43 PM

Ya I would agree with AlwaysSunny if the only other way to do this with one script would be to do a raycast from the mouse click and check if the object clicked on is a child of your object. What I would suggest though is taking Always Sunny's idea but to only have to put one script on. The way you do this is at Awake you can have your parent script go through all of its children and add the notifying component. An example might be something like this

 public class ParentObj:MonoBehaviour
 {
        void Awake()
        {
               for(int i =0;i<tranform.childCount;i++)
                {
                       GameObject g = tranform.GetChild(i).gameObject;
                       if(g.GetComponent<ChildObj>()==null)
                       {
                                 g.AddComponent<ChildObj>().parent = this;
                        }
                }
        }
       public void OnChildClick()
       {
               //Do what ever
       }
 }
 public class ChildObj:Monobehaviour
 {
         public ParentObj parent;
         void OnMouseDown()
        {
              parent.OnChildClick();
       }
 }

There might be some errors as I wrote it all here but hopefully that gets the idea of it

Comment
Add comment · Show 3 · 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 armandleggames · Mar 10, 2015 at 07:10 PM 0
Share

Thanks, that really helped!

avatar image Pendantic · Mar 10, 2015 at 07:42 PM 0
Share

Please mark as accepted if this is what you wanted

avatar image SpookySF · Jun 13, 2015 at 07:04 PM 0
Share

Did you guys try this out?

i have a prefab with a child object.

if I put this function on the child object. nothing happens void on$$anonymous$$ouseOver() { debug.log("mouse over"); }

I have only used unity for 2 weeks so I could be missing something very easy

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

How To Perform A Mouse Click On Game Object 5 Answers

Mouse controlled Laserpointer 1 Answer

OnMouseDown doesn't work 1 Answer

Clicking button locks mouse, doesn't activate button. how do I fix this? 0 Answers

Detect mouseDown & mouseUp events on handles in editor 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