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 Smaika · Feb 18, 2015 at 06:23 PM · javascriptgameobjectarray

How to set all child's of an object into an array

What I have is a game object that has several child's in the parent if the child's I have a script that has an array of GameObject public var Coins : GameObject[];

What I want is to automatically set the child's of the parent to an array

 var ParentObject : GameObject;
 var Coins : GameObject[];
 
 function Start () {
 //what should I do here to set the childs of the ParentObject on the Coins Array ?
 }

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 Firedan1176 · Feb 18, 2015 at 06:30 PM

You can use GetComponentsInChildren and grab all their transforms because (as far as I know) you cannot remove the transform from a gameObject. This is the idea (element 0 is the parent, but you can get around that):

     int value = 0;
     public Transform[] childs;
     public GameObject[] childObjects;
 
     void Start () {
         childs = gameObject.GetComponentsInChildren<Transform>();
         childObjects = new GameObject[childs.Length];
 
         foreach(Transform trans in childs) {
             value++;
             childObjects.SetValue (trans.gameObject, value - 1);
         }
     }


JS:

 value : int = 0;
 public var : Transform[] childs;
 public var : GameObject[] childObjects;
 
 function Start() {
 
 childs = gameObject.GetComponentsInChildren(Transform);
 childObjects = GameObject[childs.Length];
 
 for(var trans : Transform in childs) {
 value++;
 childObjects.SetValue(trans.gameObject, value - 1);
 
 }
 
 }

This is a robust way to do it, but it will work. I'm not sure about the JavaScript one, but the C# one works for me.

Edit

Easier way would be use Linq and Generic:

 using System.Linq;
 using System.Collections.Generic;

 void Start() {
             List<GameObject> list = new List<GameObject>();
             foreach(Transform go in obj.transform) {
                 list.Add (go.gameObject);
             }
             GameObject[] childrenObjects = list.ToArray();

}

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
0

Answer by mezzostatic · Jun 23, 2018 at 05:52 PM

This still works in unity 5.6

var go = Selection.activeGameObject;
var allChildren = go.GetComponentsInChildren(Transform); for (var child : Transform in allChildren) { print(child.gameObject.name); if(child.gameObject.GetComponent(MeshRenderer)){

                  child.gameObject.AddComponent(MeshCollider);        
 
              }    
          }

I taught myself to code at home with reaktor synth, then milkdrop, and most deeply of all, unity3D JS. I think Csharp is a waste of time for someone who just does little games and prototypes, if it can do marching cubes and mesh generation, it's enough, I hope JS works until version 2025! even if they take it off the front end.

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

22 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

Related Questions

Can't destroy Game Object in Array 2 Answers

Access a String array in one script from another script 0 Answers

NullReferenceException with object array 1 Answer

Index out of bounds even tho it shouldnt be [Javascript] 1 Answer

How do I create an array for multiple targets? 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