- Home /
Moving multiple transforms from an array in a single script
Here’s the problem. I’ve got an array of cubes that are generically spread across five layers. I want the layers 2 3 4 5 to move forward every time they see that the front layer is gone. My problem is, I cannot use coroutines when every cube is being moved on its own, so I have to try and move them in a bulk. And I don’t know how to do that.
See, the number of cubes and their position in the array can be arbitrary, but they have to move every time they see that there is a cube missing in front of them. Here’s the script which I use to detect missing cubes:
function Movement()
{
for (var fi = 0; fi <50; fi++)
{
for (var wi = 0; wi < 50; wi++)
{
for (var zi = 1; zi < 50; zi++)
{
if (createBricks.globalArray[wi,fi,zi] != null)
{
if((takeTurns.machineTurn)&&(!takeTurns.playerTurn)&&(createBricks.globalArray[wi,fi,zi].gameObject.transform.position.z != 0)&&(createBricks.globalArray[createBricks.globalArray[wi,fi,zi].gameObject.transform.position.x,createBricks.globalArray[wi,fi,zi].gameObject.transform.position.y,0] == null))
{
currentMovement = createBricks.globalArray[wi,fi,zi];
}
}
}
}
}
yield takeTurns.ReturnTurns();
}
Never mind the yield part, it just lets the game proceed once all the cubes have been processed. This function scans through the 3D array globalArray and detects missing array entries. Then we end up with a currentMovement (which is a transform) for every iteration cycle that actually detects something.
I want to move ALL the currentMovement transforms that this script detects (preferably all at once), and I’ve managed to move only one of them. I tried writing currentMovement to an array and then processing array enties, but you can’t really use a for loop here, because movement has to be in Update, but perhaps something went wrong.
For reference here’s the script that I use for actual movement routine. Maybe you will be able to point to the parts that can be modified for multiple movements, as I could not come up with anything.
function Execute()
{
if (currentMovement != null)
{
if(currentMovement.transform == createBricks.globalArray[currentMovement.transform.position.x,currentMovement.transform.position.y,1])
{
currentMovement.gameObject.transform.Translate(0, 0, -3 * Time.deltaTime);
// transform.position = Vector3.Lerp (this.transform.position, Vector3(this.transform.position.x, this.transform.position.y, 0), Time.deltaTime * smooth);
if (currentMovement.transform.position.z <= 0.01)
{
currentMovement.transform.position.z = 0;
currentMovement.gameObject.AddComponent("SingleCube");
currentMovement.gameObject.AddComponent("Detector");
createBricks.globalArray[currentMovement.transform.position.x,currentMovement.transform.position.y,0] = currentMovement.transform;
currentMovement = null;
previousMoved = true;
}
}
else if(currentMovement.transform == createBricks.globalArray[currentMovement.transform.position.x,currentMovement.transform.position.y,2])
{
currentMovement.gameObject.transform.Translate(0, 0, -3 * Time.deltaTime);
// transform.position = Vector3.Lerp (this.transform.position, Vector3(this.transform.position.x, this.transform.position.y, 1), Time.deltaTime * smooth);
if (currentMovement.transform.position.z <= 1.01)
{
currentMovement.transform.position.z = 1;
createBricks.globalArray[currentMovement.transform.position.x,currentMovement.transform.position.y,1] = currentMovement.transform;
currentMovement = null;
previousMoved = true;
}
}
else if(currentMovement.transform == createBricks.globalArray[currentMovement.transform.position.x,currentMovement.transform.position.y,3])
{
currentMovement.gameObject.transform.Translate(0, 0, -3 * Time.deltaTime);
// transform.position = Vector3.Lerp (this.transform.position, Vector3(this.transform.position.x, this.transform.position.y, 2), Time.deltaTime * smooth);
if (currentMovement.transform.position.z <= 2.01)
{
currentMovement.transform.position.z = 2;
createBricks.globalArray[currentMovement.transform.position.x,currentMovement.transform.position.y,2] = currentMovement.transform;
currentMovement = null;
previousMoved = true;
}
}
else if(currentMovement.transform == createBricks.globalArray[currentMovement.transform.position.x,currentMovement.transform.position.y,4])
{
currentMovement.gameObject.transform.Translate(0, 0, -3 * Time.deltaTime);
// transform.position = Vector3.Lerp (this.transform.position, Vector3(this.transform.position.x, this.transform.position.y, 3), Time.deltaTime * smooth);
if (currentMovement.transform.position.z <= 3.01)
{
currentMovement.transform.position.z = 3;
createBricks.globalArray[currentMovement.transform.position.x,currentMovement.transform.position.y,3] = currentMovement.transform;
currentMovement = null;
previousMoved = true;
}
}
else
{
Debug.LogError("Something went wrong in cube movenent");
print(createBricks.globalArray[currentMovement.transform.position.x,currentMovement.transform.position.y,currentMovement.transform.position.z]);
}
}
}
By answering this question you would be saving my ass, so thank you very much if you have read it to this point. I’m completely stuck and don’t know what to do next.