- Home /
finding an object with tag, out of a few objects that are in array
so I've got this code
function enparenting(){
//looking for central piece
for(var cube:Transform in slice){
if(cube.CompareTag("centralCube")){
pivot = cube;
}
}
for(var child: Transform in pivot) child =null;
for(var cube: Transform in slice) cube.parent = pivot;
}
to make the cube which is tagged "centralCube" a parent of all the other cubes that are in an array called "slice". ("central cube" is a part of the slice array, I should mention)
but the way I do it above doesn't really change parenting of the central cube I can make the code work if I do it this way
var pivot: Transform;
function enparenting(){
for(var child: Transform in pivot) child =null;
for(var cube: Transform in slice) cube.parent = pivot;
}
and set "pivot" in unity editor, at the set up of the scene, but then again, I can't afford to have a pre-set pivot selected from the very start. It will change.
Answer by skovacs1 · Oct 06, 2010 at 02:35 PM
The reason your first script won't work is that you are setting the child to null. You want to set the child's parent to null. This should work for you:
function enparenting(){ //looking for central piece for(var cube:Transform in slice) if(cube.CompareTag("centralCube")) pivot = cube;
for(var child: Transform in pivot) child.parent = null;
for(var cube: Transform in slice) cube.parent = pivot;
}
If you are doing a Rubik's, beware when doing the center slice (if you have one) because the center slice will also contain the central pivots for the other sides which would all need their parenting cleared before you rotate the center and you would need to identify the actual centerCube specifically in this case.
function enparenting(){ //looking for central piece for(var cube:Transform in slice) { if(cube.CompareTag("centralCube")) pivot = cube; else if(cube.CompareTag("centerCentralCube")){ pivot = cube; break; } }
for(var cube: Transform in slice) cube.parent = pivot;
//do something with the pivot
for(var cube: Transform in slice) cube.parent = null; //or some shared parent
}
Your answer
