I have a card prefab that gets generated 6 times whenever you level up in my game. The goal is for you to be able to click them, and they will switch from a white border to a blue border, you can click another card, and the first card will return to a white border, and the new card will switch to blue, and you can click different set areas in the menu, which will put the card on that location. I would also like to make it so when you click a second card, the two cards switch places. This is the relevant code that I have:
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using TMPro;
public class LevelUpMenu : MonoBehaviour
{
//
void DrawCards()
{
GameObject a = null;
for (int i = 0; i < 6; i++)
{
a = Instantiate(cardBase);
a.GetComponent<CardContainer>().data = cardData[Random.Range(0, cardData.Length - 1)];
a.transform.SetParent(parentPos[i + 5].transform, false);//the parentPos array contains the locations where the objects can go
a.GetComponent<CardContainer>().icon.sprite = a.GetComponent<CardContainer>().data.icon;
a.GetComponent<CardContainer>().title.text = a.GetComponent<CardContainer>().data.cardName;
a.GetComponent<Button>().onClick.AddListener(() => CardClicked(a));//this is one of my attempts to get the cards to register that they have been clicked
}
}
public void CardClicked(GameObject card)
{
moving = card;
card.GetComponent<CardContainer>().data.color2 = Color.blue;
foreach (GameObject c in cards) //cards is a list of the card prefabs
{
if (c != card)
{
c.GetComponent<CardContainer>().data.color2 = Color.white;
}
card.transform.GetChild(1).gameObject.GetComponent<Image>().color = card.GetComponent<CardContainer>().data.color2;
}
}
public void SlotClicked(GameObject slot)
{
if (moving != null)
{
moving.transform.SetParent(slot.transform);
moving.transform.position = moving.transform.parent.position;
moving.GetComponent<CardContainer>().data.color2 = Color.white;
moving.transform.GetChild(1).gameObject.GetComponent<Image>().color = moving.GetComponent<CardContainer>().data.color2;
moving = null;
}
}
}
using UnityEngine;
using TMPro;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class CardContainer : MonoBehaviour
{
public CardBox data;
public Image icon;
public TMP_Text title;
}
I put the slot function into a button component that is on a number of game objects, the six spaces where the cards are instantiated, and the five slots for game stuff. Any help that can be provided would be appreciated.