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 zak666 · Mar 07, 2017 at 09:29 AM · transformchildren

Transform [] = all children of this game object ? (Spawnpoints)

their has to be an easier way then to drag and drop 30+ Game Objects :)

I have "Spawn points" for where enemies can spawn, Can I have all my GameObjects under a ""Master"" Game Object and go Transform[] = and get al the Children transforms? Drag and dropping for every spawn point is ridiculous their has to be a simpler way of doing this :)

 [SerializeField]
     private Transform[] spawnPoints; // drag and drop in Inspector
     private  Transform spawnPointTransform;
 
     public FighterCounter FighterCount;
     public GameObject MissionControll;
 
     public bool destroyer;
     public bool BattleShip;
     public bool Carrier;
     public bool Fighter;
 
 
     public bool OurTeam;
 
     void Start(){
         FighterCount = MissionControll.gameObject.GetComponent<FighterCounter> ();
     }
 
     // Use this for initialization
     void LateUpdate () {
         if (PhotonNetwork.isMasterClient) {
 
             if(FighterCount.TeamOneFighters > 0 && Fighter == true && OurTeam == true){
                 int index = Random.Range(0, spawnPoints.Length);
                 spawnPointTransform = spawnPoints[index];
                 PhotonNetwork.Instantiate ("T1FighterA", spawnPointTransform.position, spawnPointTransform.rotation, 0);
                 FighterCount.TeamOneFighters -= 1;
             }
 }
 
 
 
 
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 Kossuranta · Mar 07, 2017 at 10:26 AM 2
Share

You already got your answer, but you can also select all of them at once and then drag & drop in inspector. Clicking the small lock icon at the top right corner of inspector will lock the currently selected object so it doesn't change while you are selecting items that you want to drag.

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by HarshadK · Mar 07, 2017 at 09:38 AM

This should do it:

 public Transform SpawnPointContainer;      // Assign parent gameobject of spawn points.
 private Transform[] spawnPoints;
 
 void Start()
 {
     // Initialize spawnPoints array.
     spawnPoints = new Transform[spawnPointContainer.childCount];
     // Populate array with child transforms.
     for(int i = 0; i < spawnPointContainer.childCount; i++) {
          spawnPoints[i] = spawnPointContainer.GetChild(i);
     }
 }

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 ExtinctSpecie · Mar 07, 2017 at 09:38 AM

yes you can have an array of all child objects could be something like this

 public static Transform[] spawnPoints;
 
 spawnPoints = new Transform[transform.childCount];
 for (int i = 0; i < spawnPoints.Length; i++)
 {
     spawnPoints[i] = transform.GetChild(i);
 }
 //this script has to be attached to the parent object otherwise you will have to pass the Transform component of that parent object
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 Bunny83 · Mar 07, 2017 at 11:54 AM

Here's a one-liner:

 // at the top
 using System.Linq;

 void Start()
 {
     spawnPoints = spawnPointTransform.Cast<Transform>().ToArray();
 }

However essentially it does the same as the code that HarshadK or ExtinctSpecie posted, just in one line using Linq. Note that even though this looks "shorter" it's not "faster" or "better". It's actually a bit worse from a performance point of view. I just posted the solution for those who prefer "short code" ^^.

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

77 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how to find, not a specific child (GetChild(0)), but a range of child objects (GetChild(0 to 4)) 1 Answer

How to move the parent object to a child of one of its children? 1 Answer

Character picking up box 0 Answers

get bone path from transform 1 Answer

Move parent so children align with other obj! 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