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 CleeDotExe · Oct 06, 2021 at 08:25 AM · unity 2dgameobjects

Filling array with children of multiple undefined gameobjects?

Hi! So I want to find all available transforms on a buttonpress, and put them in an array to randomise a spawning point. alt text

I want to make a spawning randomiser of a pipelines, but I need the transforms of those children. Since it's not one GameObject I can't say 'FindComponentInChildren' or anything alike. All the children have tags to their name accordingly, so I can define when instantiating an object where it instantiated. here's my code:

   GameObject[] HasTopTrans;
   GameObject[] HasBottomTrans;
   GameObject[] HasRightTrans;
   GameObject[] HasLeftTrans;
   public GameObject[] Arrrr;
   
   

   public void OnClick()
   {
       //This would work, if the objects weren't children. It just collects all the transforms and puts 
       //them in one array. I'm using "using System.Linq" too.
       HasTopTrans = GameObject.FindGameObjectsWithTag("Up");
       HasBottomTrans = GameObject.FindGameObjectsWithTag("Down");
       HasRightTrans = GameObject.FindGameObjectsWithTag("Right");
       HasLeftTrans = GameObject.FindGameObjectsWithTag("Left"); 
       Arrrr = HasTopTrans.Concat(HasBottomTrans).ToArray();
       Arrrr = Arrrr.Concat(HasRightTrans).ToArray();
       Arrrr = Arrrr.Concat(HasLeftTrans).ToArray();

   }
capture.png (13.0 kB)
Comment
Add comment · Show 8
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 Hellium · Oct 06, 2021 at 08:29 AM 1
Share

Your question is not perfectly clear.

  1. Which Transforms you want to collect? The children of the pressed button? Why FindComponentsInChildren ($$anonymous$$d the 's') wouldn't work?

  2. Where is this script attached?

  3. How and when is OnClick called?

  4. Are Talu$$anonymous$$ium, skewalu$$anonymous$$ium and 4wayalu$$anonymous$$ium buttons?

avatar image CleeDotExe Hellium · Oct 06, 2021 at 09:19 AM 0
Share

@Hellium 1. The transforms of the prefabs, which get randomly spawned at any available spots on the buttonpress, I want to fill an array with all available transforms, then randomly pick one and spawn a pipe on there, move the pipe according to which tag the transform it spawned on has. FindComponentsInChildren wouldn't work since the prefabs aren't defined, unless there's a way to make an array, fill those with all the prefabs that are currently on the map and scan them for available transforms. 2. On the button, I'm only calling a void which does this on ButtonPress, it'd be illogical to put it on every prefab. 3. When I click the button. 4. They are the prefab pipes which are randomly generated as I described in 1.

I'm sorry if this is a second message, but my first reply didn't show up.

avatar image Hellium CleeDotExe · Oct 06, 2021 at 09:27 AM 1
Share

Ok, thanks for the precisions.


What does not work with your current code? GameObject.FindGameObjectsWithTag is able to find every active object with the given tag, regardless of whether they are children.


You could also ask your spawner script to maintain a list of the children transforms when spawning a new pipe.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by LucasMartin · Oct 06, 2021 at 01:48 PM

Supposing that the tranforms that you want to use belong to the objects "Up", "Down", "Left" or "Right", you could try something different:

     Transform[] childTransforms;
     Transform chosenTransform;
 
     private void Start()
     {
         childTransforms = new Transform[transform.childCount];
     }
 
     public void OnClick()
     {
         for (int x = 0, i = transform.childCount; x < i; x++)
             childTransforms[x] = transform.GetChild(x);
 
         chosenTransform = childTransforms[Random.Range(0, childTransforms.Length)];
     }

Then, all you have to do is to use the variable chosenTransform wherever you want.

Comment
Add comment · Show 3 · 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 CleeDotExe · Oct 06, 2021 at 09:38 PM 0
Share

Thanks for the reply, I used some of the code ideas in the reply I wrote in the reply of my original question, in this script however, it seems like you're getting all children in the whole 'world'?

avatar image LucasMartin CleeDotExe · Oct 09, 2021 at 11:30 PM 0
Share

Sorry for taking too long. My code gets all children of the object the code is attached to, so this might help you :)

avatar image CleeDotExe LucasMartin · Oct 11, 2021 at 05:47 AM 0
Share

Thanks! But I'd rather do the command line Hellium gave, since that way I don't need every clone to have a script!

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

137 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 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

destroy prefabs when they hit the player 1 Answer

Same script not working on next level 0 Answers

If i flip the scale from a sprite, how do i make my game object child flip aswell? 0 Answers

How to check which gameobject is the player using 2 Answers

Spawn GameObject Above and Below GameObject 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