I have a set of game objects with a value (called ID) in an attached script.
is it possible to select all the game objects with the same value and place them into an array?
If they all had the same tag I could do it with the following code:
allSegments = GameObject.FindGameObjectsWithTag ("Segment");
However, tagging is not an option, so im looking for a solution which can use the vairable in an attached script. Something like
allSegments = GameObject.FindGameObjectsWithComponent<script>.id == 4;
is this possible?
You must write youself function. For example, all your objects have one parent object by name “Contener”. Than find all objects with same “MyScript” with value id== 4. I write example(write on CSharp):
private List<Transform> myList = new List<Transform>(); //our list for all object with correct tag
void Start() {
myList.Clear(); //clear list
this.myFindChild(GameObject.Find("Contener").transform, 4); //find all objects with id==4
}
//recurrent functions
public void myFindChild(Transform tpParent, int tpId) {
for(int i = 0; i < tpParent.childCount; i++) { //See all child and see they child
if (tpParent.GetChild(i).GetComponent<myScript>() != null) { //check scripts of child
if(tpParent.GetChild(i).GetComponent<myScript>().id == tpId)
myList.Add(tpParent.GetChild(i));
}
}
//See childs of current child
this.myFindChild(tpParent.GetChild(i), tpTag);
}
}
I hope that it will help you.