- Home /
The question is answered, right answer was accepted
How do I swap gameObjects on a list by clicking in the UI?
I'm making a game where the player moves a certain piece by rearranging "movement cards" in order to create the path he wants the piece to take to reach the end goal. The problem is, I don't know how to do the rearranging part.
Currently, I have a UI element that has a fixed amount of Images that change sprite according to the list information and are given a random order and I'd like to know how I could make the player rearrange said pieces by clicking or dragging on the images and swapping them with the other images [example in the image]. (Swap those two pieces) Here's the code I have right now, and I honestly have no idea where to go from here. I'm very new to programming and I've been teaching myself only through experimenting so please don't go too hard on me :P
void DarPeçasNecessarias() //Gives the exact amount of necessary pieces to finish the level
{
if (direçao.x > 0)
{
numRight = (int) direçao.x;
for (int i = 0; i < numRight; i++)
{
Peçass.Add("Right");
}
if (direçao.y > 0)
{
numUp = (int) direçao.y;
for (int i = numRight; i < numRight + numUp; i++)
{
Peçass.Add("Up");
}
}
else if (direçao.y < 0)
{
numDwn = (int) - direçao.y;
for (int i = numRight; i < numRight + numDwn; i++)
{
Peçass.Add("Down");
}
}
else { numDwn = 0; numUp = 0; }
}
else if (direçao.x < 0)
{
numLft = (int) - direçao.x;
for (int i = 0; i < numLft; i++)
{
Peçass.Add("Left");
}
if (direçao.y > 0)
{
numUp = (int) direçao.y;
for (int i = numLft; i < numLft + numUp; i++)
{
Peçass.Add("Up");
}
}
else if (direçao.y < 0)
{
numDwn = (int) - direçao.y;
for (int i = numLft; i < numLft + numDwn; i++)
{
Peçass.Add("Down");
}
}
else { numDwn = 0; numUp = 0; }
}
else { numLft = 0; numRight = 0; }
//-----------------
numPeçasNecessarias = numRight + numLft + numUp + numDwn;
DarPeçasExtra();
}
void DarPeçasExtra() //Gives extra pieces
{
for (int i = numPeçasNecessarias; i < NumPeças; i++)
{
int nrg = Random.Range(1,5);
//Debug.Log(nrg);
if (nrg == 1)
{
Peçass.Add("Up");
numUp++;
}
else if (nrg == 2)
{
Peçass.Add("Down");
numDwn++;
}
else if (nrg == 3)
{
Peçass.Add("Left");
numLft++;
}
else
{
Peçass.Add("Right");
numRight++;
}
}
ShuffleList();
}
void ShuffleList() //Shuffles the List
{
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[i];
Peçass[i] = Temp;
}
FillImages();
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.P))
{
canMove = !canMove;
}
if (canMove)
{
if (!isRunning)
{
StartCoroutine(AplicarMovimento());
}
}else { StopCoroutine(AplicarMovimento()); isRunning = false; }
AbrirHotbar();
FillImages();
}
void AbrirHotbar()//Opens the "hotbar" where all the movement cards are located
{
if (Input.GetMouseButtonDown(0))
{
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 mousePos2D = new Vector2(mousePos.x, mousePos.y);
RaycastHit2D hit = Physics2D.Raycast(mousePos2D, Vector2.zero);
if (hit.transform.gameObject.CompareTag("TilePeça") && !Hotbar.activeSelf)
{
Hotbar.SetActive(true);
}
else if (hit.transform.gameObject.CompareTag("TilePeça") && Hotbar.activeSelf)
{
Hotbar.SetActive(false);
}
}
}
void FillImages() // Changes the sprite of the images in the Hotbar
{
for (int i = 0; i < Peçass.Count; i++)
{
Transform botao = Hotbar.transform.GetChild(i);
if (Peçass[i] == "Up")
{
botao.GetComponent<Image>().sprite = Up;
}else if (Peçass[i] == "Down")
{
botao.GetComponent<Image>().sprite = Down;
}
else if (Peçass[i] == "Left")
{
botao.GetComponent<Image>().sprite = Left;
}
else if (Peçass[i] == "Right")
{
botao.GetComponent<Image>().sprite = Right;
}
}
}
Answer by AbandonedCrypt · Feb 06, 2021 at 03:03 PM
Get the list index of both the source object and a temp variable, then swap them by index.
List<Object> list = new List<Object>();
int target, source; //index of target and source object
Object tmp; //temporary store the target object
void SwapObjects()
{
tmp = list[target];
list[target] = list[source];
list[source] = tmp;
}