Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Elisa93 · Nov 04, 2016 at 10:25 PM · colliderparent

How to write a script for a gameObject that refers to all children?

HI!! I'm working on a project (a church) in Unity because I want to put it into the Dive. I'm trying to write a script for the parent (that is all my church exported from 3dSMax) that refers to every child (walls, windows, roofs...). I would like that clicking an object or better a child (a wall, a window..) a box appears with some information inside: how can I do this? Every child is a mesh collider in Unity and I have to work with Raycast and 3Dtext, because this project will be visible in the DIVE. I would be very happy if you could help me out with some information about this problem. Thanks a lot.

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
0

Answer by jmgek · Nov 04, 2016 at 11:55 PM

 gameObject.GetComponentsInChildren<Transform>(); //To get the scripts

 //Or

 List<gameObject> newList = new List<gameObject();
 
 foreach (Transform child in Church) 
     newList.Add(child);
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 Elisa93 · Nov 06, 2016 at 02:33 PM 0
Share

Thank you very much!!

avatar image
0

Answer by frederik99 · Nov 05, 2016 at 12:13 PM

I hope i got it right

 using UnityEngine;
 
 public class ChurchChildrenInfoDisplay : MonoBehaviour {
 
     public ChildInformation[] childInformationArray;
 
     private void Start() {
 
         //usage example
         childInformationArray = new ChildInformation[] {
 
             new ChildInformation(GameObject.Find("Wall").transform, "This is a wall."),
             new ChildInformation(GameObject.Find("Window").transform, "This is a window.")
         };
     }
 
     private void Update () {
     
         if (Input.GetMouseButtonDown(0)) {
 
             Ray mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
             if (Physics.Raycast(mouseRay, out hit)) {
 
                 Transform hitTransform = hit.transform;
                 ChildInformation hitChildInfo = GetChildInformationOfTransform(hitTransform);
                 if (hitChildInfo != null) {
 
                     OnChildClicked(hitChildInfo);
                 }
             }
         }
     }
 
     private ChildInformation GetChildInformationOfTransform(Transform child) {
 
         foreach (ChildInformation childInfo in childInformationArray) {
 
             if (childInfo.transform == child) {
 
                 return childInfo;
             }
         }
 
         return null;
     }
 
     private void OnChildClicked(ChildInformation childInfo) {
 
         //display a box with some information inside
     }
 }
 
 [System.Serializable]
 public class ChildInformation {
 
     public ChildInformation() {
 
     }
 
     public ChildInformation(Transform transform, string info) : this() {
 
         this.transform = transform;
         this.info = info;
     }
 
     public Transform transform;
     
     //this can be any (or several) datatype(s)
     public string info;
 }
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

Separating behavior of parent and child Colliders: 1 Answer

Having trouble understanding parenting with componants 0 Answers

onTriggerEnter is unclear to me... 3 Answers

How to collide children of a Rigidbody installed parent object. 0 Answers

How to detect child object collisions on parent 3 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