- Home /
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!
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 ?
Just once at start and then destroy/disable them in some function.
So Clunk47's answer would be the one you are looking for :)
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.
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!
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;
}
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);
}
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);
}
}
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.
Gimme a sec and I'll edit out the C# example and edit the JS example to remove from lists.
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
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.
The code errors out with 'gameObject' is not a member of 'Object' :s
Your answer
Follow this Question
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