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
0
Question by Josh 7 · Sep 10, 2010 at 06:45 PM · gameobjectparentchildmouse-drag

All encompassing Game Object.

Can I make a game object (Parent) that holds all or all of a type of game object and then attack a script to the Parent object and it would apply to each child or just apply to the group?

For example I want to make a chess game. Can all the chess pieces be contained in a Chess Pieces parent object and just have a mouse drag script attached to the Parent and it would work on each individual piece or is that not efficient?

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 joedrigon · Sep 10, 2010 at 08:30 PM 0
Share

I am doing something similar and I attached my mouse drag script to each object because I felt it needed to be. I'd be interested in knowing about doing it this way also.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by skovacs1 · Sep 10, 2010 at 09:12 PM

attacking a script? is that like attacking the darkness? :P

The answer to the first part is that you can attach a script to a parent game object and have the script apply to either the parent and/or each child because you can loop through the children:

for(var child : Transform in transform) {
    //...do something with each child
}

If you wanted to have the editor set a script on each of the children, you would do it in much the same way, but you would put the code in an editor script in stead.

The answer to the second part is that if you did it like the above loop, you can do stuff to each of the chess pieces, but it's not the best idea for something like a mouse drag. Events I believe only fire to the scripts attached to the object that fired them, therefore if you were referring to an OnMouseDrag() function or something, it would only fire for the collider of the object to which it was attached (the parent). For your specific example of the mouse drag, to do it like the above, you would have to raycast against each child individually, which can be costly and isn't really necessary as one raycast against all of them would do (put them on the same layer or something and just raycast that layer if you only want to check against them).

var hit : RaycastHit;

if(Input.getMouseButtonUp(0)) //stop dragging and return else if(Input.getMouseButtonDown(0) && Physics.Raycast(mainCamera.transform.position, mainCamera.transform.forward, hit)) //check against hit.transform and start dragging if(Input.getMouseButton(0)) //drag

But this doesn't really have to be in a parent and can be anywhere (usually people put this stuff on a camera or some empty gameobject).

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

No one has followed this question yet.

Related Questions

Make a simple tree 1 Answer

How can I access a child object? 1 Answer

Instantiate Terrain Object as child of Empty Game Object 1 Answer

How to destroy a parent object when child object is collided 1 Answer

Creating new Transform from existing objects Transform to Instantiate object 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