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!
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.