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
4
Question by pretender · Jun 19, 2010 at 02:07 PM · collisionparentchild

detect collision on child

hi everybody, i am trying to figure out one thing, that seemed easy at first, though i am just a beginner.

i want to have only one script in the parent gameobject, and i will have four child and i want to detect when user clicked any of the children...is this possible without having scripts for every child?

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
4
Best Answer

Answer by Tetrad · Jun 19, 2010 at 04:22 PM

Sure it's possible, but it would be a lot easier if you put scripts on them. You can even put scripts on children dynamically.

If you wanted to do it without child scripts, you would simply iterate over your children and then do a raycast on their collider with the screen point to ray.

However, if you wanted to let unity handle it, and do dynamic child scripts, here's one (slightly advanced) way to go about doing it.

Parent class:

void Awake() { foreach( Transform childTransform in transform ) { ChildClass childClass = childTransform.AddComponent<ChildClass>(); childClass.TouchDelegate = OnTouchedChild; } }

void OnTouchedChild( GameObject childObject ) { Debug.Log( "touched child " + childObject.name, childObject ); // do whatever }

Child class (let's say it's called ChildClass, but you can name it whatever as long as it's updated in the parent class script):

public delegate void OnTouchedDelegate( GameObject touchedObject );

OnTouchedDelegate touchDelegate; public OnTouchDelegate TouchDelegate { set { touchDelegate = value; } }

void OnMouseDown() // monobehaviour method { touchDelegate( gameObject ); }

You might want to put some error handling in there, like maybe check to see if the touch delegate is not null if that's a case you want to handle, or if the child object has a collider before adding the script, or whatever your specific needs are.

Comment
Add comment · Show 4 · 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 Ashkan_gc · Jun 19, 2010 at 05:23 PM 0
Share

doing that easy you should cast a ray and see who is colliding with the ray. cast the ray using the functions that tetrad said but hey the advantage of unity is it's aggregation system and components so use the second method that he said unless there is a good reason.

avatar image Jean-Fabre · Dec 21, 2010 at 10:27 AM 0
Share

Nice code, learned a lot, thanks tetrad

avatar image Jean-Fabre · Dec 21, 2010 at 11:06 AM 0
Share

one thing tho for reference, addComponent only works using gameObject reference and on the ChildClass, it's public OnTouchedDelegate ( not on touchDelegate). then it all works fine.

avatar image numberkruncher · Mar 28, 2012 at 11:20 PM 0
Share

@Jesse How well does raycasting perform? I would like to choose the most optimal of these two choices. I am more comfortable with the raycasting approach except I am unsure as to what the consequences are. Cheers

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

No one has followed this question yet.

Related Questions

Make a simple tree 1 Answer

Ignore parent Rotation 1 Answer

Instantiated GameObject collision without script repetition? 1 Answer

Child object not moving with parent 0 Answers

How to stop child movement due to collision. 0 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