# How do I swap two gameObjects order in a list?

So I have this list filled with “instructions” for tile-based movement, but currently it is generated randomly and you cannot swap the positions of said instructions so the gameObjects just go around and move randomly. I’d like to be able to move those instructions around in order for the player to be able to move those gameObjects in their desired order.

This is how the movement “cards” are given and shuffled:

``````void DarPeçasNecessarias() //Da exatamente as peças necessárias para o jogador terminar o nivel
{
if (direçao.x > 0)
{
numRight = (int) direçao.x;
for (int i = 0; i < numRight; i++)
{
}
if (direçao.y > 0)
{
numUp = (int) direçao.y;
for (int i = numRight; i < numRight + numUp; i++)
{
}
}
else if (direçao.y < 0)
{
numDwn = (int) - direçao.y;
for (int i = numRight; i < numRight + numDwn; i++)
{
}
}
else { numDwn = 0; numUp = 0; }

}
else if (direçao.x < 0)
{
numLft = (int) - direçao.x;
for (int i = 0; i < numLft; i++)
{
}
if (direçao.y > 0)
{
numUp = (int) direçao.y;
for (int i = numLft; i < numLft + numUp; i++)
{
}
}
else if (direçao.y < 0)
{
numDwn = (int) - direçao.y;
for (int i = numLft; i < numLft + numDwn; i++)
{
}
}
else { numDwn = 0; numUp = 0; }
}
else { numLft = 0; numRight = 0; }
//-----------------
numPeçasNecessarias = numRight + numLft + numUp + numDwn;
DarPeçasExtra();
}

void DarPeçasExtra() //Da o resto das peças
{
for (int i = numPeçasNecessarias; i < NumPeças; i++)
{
int nrg = Random.Range(1,5);
//Debug.Log(nrg);
if (nrg == 1)
{
numUp++;
}
else if (nrg == 2)
{
numDwn++;
}
else if (nrg == 3)
{
numLft++;
}
else
{
numRight++;
}
}

ShuffleArray();
}

void ShuffleArray() //Baralha o array
{
string Temp = "Temp";
for (int i = 0; i < Peçass.Count; i++)
{
int rnd = Random.Range(0, Peçass.Count);
Temp = Peçass[rnd];
Peçass[rnd] = Peçass*;*
``````

Peçass = Temp;
}
//FillImages();
}
I’d like to know where do I go from here, currently there are temporary visual indicators that show the order of the cards and movement, but I’d like to move them around so that it can move freely, eg:
[175095-example.png|175095]_
_

Any help would be greatly appreciated! Thank you!

To swap items in an array or list, create a temporary value that stores variable a, set a to b, and set b to that temporary value. For example:

``````    void Swap(GameObject a, GameObject b)
{
GameObject temp = a;
a = b;
b = temp;
}
``````

@DiasPTW Is that what you needed? Your question sounds a little complicated.

It’s not so clear to me what your question is.
You obviously know how to swap `List<T>` items, as you do the same in your `ShuffleArray` function.
Nor do I really get what `DarPeçasNecessarias` does. Is it called on Clicking the gameObjects?

I see a bunch of “instructions” in your screenshot, these are each GameObjects with an `InstructionScript` attached I’d guess? But do they also know which position they have in the “List”, I assume they are executed left-to-right, top-to-bottom?

In that case you would want these gameObjects to swap positions visually, and in the way you keep track of them, e.g.

``````Vector3 posTemp = objectOne.transform.position;
objectOne.transform.position = objectTwo.transform.position;
objectTwo.transform.position = posTemp;

var scriptOne = objectOne.GetComponent<InstructionScript>();
var scriptTwo = objectTwo.GetComponent<InstructionScript>()
Swap(scriptOne.Number, scriptTwo.Number);
``````

@DiasPTW Do you need something like this?