I have a idiot problem :
How to add a value in a variable of type Transform [ ] in a loop in C#?
public Transform[] objectT;
foreach(Transform t in transform){
objectT. ???? Add t ??? ;
}
I have a idiot problem :
How to add a value in a variable of type Transform [ ] in a loop in C#?
public Transform[] objectT;
foreach(Transform t in transform){
objectT. ???? Add t ??? ;
}
[ ] style arrays are fixed size. I.e. when created you specify a size, and they get no larger or smaller. This means you need to set objectT’s size before looping:
public Transform[] objectT = new Transform[transform.childCount]; //This makes the size of the array the same size as the number of children this object has.
int i = 0;
foreach(Transform t in transform){
objectT[i++] = t; //This puts 1 transform in each open slot in the array.
}
Ok, thank you;)
To solve the problem I go through a temporary array, and use
public Transform[] path;
private void addInPath(Transform node){
Transform[] pathTemp = new Transform[path.Length+1];
path.CopyTo(pathTemp, 0);
pathTemp[path.Length] = node;
path = new Transform[path.Length+1];
pathTemp.CopyTo(path, 0);
}
Alternately you could just use a List and save the hassle of allocating a new array all the time.