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 ChrisSch · Sep 29, 2013 at 05:44 PM · arraylistparticlechildrenfill

List children with name in array?

Ok I've been trying to do that the whole day and I can't get it to work. x'D

I don't have one specific code cause I tried countless variations I found on google and unity answers and none were exactly what I was looking for. I refrained from asking sooner cause I thought I'd find it and cause I'm asking stuff here basically every couple days. xD

I have an object and a script on it and all I want is for it to find its children named "Laser", and put them in an array, cause I want to destroy them, or deactivate their particle emitter, in another function when something happens.

How can I do that?

EDIT: Thank you all for all the answers it really means a lot!

Comment
Add comment · Show 3
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 KiraSensei · Sep 29, 2013 at 05:54 PM 0
Share

Do you need your list to be updated sometimes (the children can change, or you add some if the user does something), or do you create the list once for all ?

avatar image ChrisSch · Sep 29, 2013 at 05:58 PM 0
Share

Just once at start and then destroy/disable them in some function.

avatar image KiraSensei · Sep 29, 2013 at 06:18 PM 1
Share

So Clunk47's answer would be the one you are looking for :)

3 Replies

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

Answer by AjayKhara · Sep 29, 2013 at 06:17 PM

Try something like this.

 import System.Collections.Generic;
 
 #pragma strict
 
 var objectName : String = "Laser";
 
 //parent object transform
 var parent : Transform;
 
 // list (array)
 var myList : List.<Transform> = new List.<Transform>();
 
 function FetchObject(){
     
     // loop through all the child transforms of the parent.
     for (var child : Transform in parent) {
     
         //if the child transform name is same as wanted, add it to the list(array)
         if(child.name.Equals(objectName)){
             myList.Add(child);
         }
         
     }
 
 }
 
 function ProcessObject(){
 
     //myList[0].GetComponent(anycomponent).dosomething;
 
 }


You can use a generic collection. I have used list, you can use anything you like.

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 ChrisSch · Sep 29, 2013 at 07:09 PM 0
Share

Your way seems to do what I want thank you, and thank you everyone else for answering!

I just added parent = gameObject.transform; at start so I don't have to assign it every time since I have 60 levels and in some there's up to 10 of these objects, with 4 children each, so doing things automaticaly means a lot. xD

Also I wrote: myList[0].GetComponent(ParticleEmitter).emit=false; myList[1].GetComponent(ParticleEmitter).emit=false; myList[2].GetComponent(ParticleEmitter).emit=false; myList[3].GetComponent(ParticleEmitter).emit=false;

4 times cause there's always 4 children, and idk a diff way of doiong it, and I just wanted to destroy/disable them, and I disabled the emitting in this case. Thanks again!

avatar image AjayKhara · Sep 30, 2013 at 02:30 AM 0
Share

Glad I could help. Ok, you did the right thing here, if you do not want to write 4 lines for disabling each particle emitter, you can loop through the list, and disable them all at once.

 for (var item: Transform in myList) {
  
 item.GetComponent(ParticleEmitter).emit=false;
  
 }
avatar image AjayKhara · Sep 30, 2013 at 02:43 AM 0
Share

to destroy each laser, you can use this.

 while (myList.Count > 0) {
     
         //disable particle emitter.
         myList[0].GetComponent(ParticleEmitter).emit=false;
         
         //asssign it to temporary var.
         var tempItem : Transform = myList[0];
         
         //remove item from the list.
         myList.RemoveAt(0);
         
         //destroy item.
         Destroy (tempItem.gameObject);
 
     }
avatar image
2

Answer by clunk47 · Sep 29, 2013 at 06:04 PM

A JS Example:

 #pragma strict
 
 import System.Collections.Generic;
 
 var Lasers : List.<Transform>;
 
 function Awake()
 {
     for(var t : Transform in transform)
     {
         if(t.name == "Laser")
             Lasers.Add(t);
     }
     Debug.Log(Lasers.Count);
 }
 
 function Update()
 {
     if(Input.GetKeyDown(KeyCode.E))
     {
         if(Lasers.Count > 0)
         {
             Lasers.RemoveAt(0);
         }
         else print("No more items in list.");
         print(Lasers.Count);
     }
 }
Comment
Add comment · Show 4 · 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 ChrisSch · Sep 29, 2013 at 06:30 PM 0
Share

Damn, so much help, thank you all. xD How do I destroy the Lasers I got? The count works but Destroy(Lasers) doesn't work. :S

Using javascript, always forget to mention.

avatar image clunk47 · Sep 29, 2013 at 06:34 PM 0
Share

Gimme a sec and I'll edit out the C# example and edit the JS example to remove from lists.

avatar image ChrisSch · Sep 29, 2013 at 06:43 PM 0
Share

Ok yes sure. :s

avatar image ChrisSch · Sep 29, 2013 at 06:53 PM 0
Share

Thanks for the update but if I'm not mistaken, that only removes them from the list, while I want to destroy/disable those game objects. :s

avatar image
-1

Answer by Professor Snake · Sep 29, 2013 at 06:03 PM

You can either manually reference them in an array in a prefab of your object, or use:

 for(var childObj in transform){
   if(childObj.name=="Laser")
     //do things
 }



I'd recommend the former since getting gameobjects by their name can be unreliable.

Comment
Add comment · Show 2 · 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 ChrisSch · Sep 29, 2013 at 06:08 PM 0
Share

The code errors out with 'gameObject' is not a member of 'Object' :s

avatar image Professor Snake · Sep 29, 2013 at 06:39 PM 0
Share

Hmm, then childObj.name should work.

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

19 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

Related Questions

A node in a childnode? 1 Answer

array of childern 1 Answer

how to define spesific object in list? 2 Answers

Array to create a list of objects in the scene? 1 Answer

wont generate correctly 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