- Home /
Changing from Sprite Array to Game Objects
Hi there, I'm currently working on a program that is using a Sprite Array to change the cardFaces and cardBacks of the cards. For my current purposes I need to change the Sprites to Game Objects. I have the program working when using Sprites, but I'm currently struggling changing it to accept Game Objects instead. I currently have two classes, Gameplay dealing with the logic of the card dealing and Update Sprite which changes the cardFaces and cardBacks of each card. I've been trying to change those objects to Game Objects instead of Sprites but have been struggling to do so.
public class Gameplay : MonoBehaviour
{
public Sprite[] cardFaces;
public GameObject cardPrefab;
public GameObject deckButton;
public GameObject[] handPos;
public static string[] suits = new string[] { "A" };
public static string[] values = new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21" };
public List<string>[] hands;
public List<string> drawOnDisplay = new List<string>();
public List<List<string>> deckDraw = new List<List<string>>();
private List<string> hand0 = new List<string>();
public List<string> deck;
public List<string> discardPile = new List<string>();
private int deckLocation;
private int draw;
private int drawRemainder;
// Start is called before the first frame update
void Start()
{
hands = new List<string>[] { hand0 };
PlayCards();
}
// Update is called once per frame
void Update()
{
}
public void PlayCards()
{
deck = GenerateDeck();
Shuffle(deck);
//test the cards in the deck:
foreach (string card in deck)
{
//print(card);
}
Sort();
StartCoroutine(Deal());
SortDeck();
}
public static List<string> GenerateDeck()
{
List<string> newDeck = new List<string>();
foreach (string s in suits)
{
foreach (string v in values)
{
newDeck.Add(s + v);
}
}
return newDeck;
}
void Shuffle<T>(List<T> list)
{
System.Random random = new System.Random();
int n = list.Count;
while (n > 1)
{
int k = random.Next(n);
n--;
T temp = list[k];
list[k] = list[n];
list[n] = temp;
}
}
IEnumerator Deal()
{
for (int i = 0; i < 1; i++)
{
float yOffset = 0;
float zOffset = 0.03f;
foreach (string card in hands[i])
{
yield return new WaitForSeconds(0.01f);
GameObject newCard = Instantiate(cardPrefab, new Vector3(handPos[i].transform.position.x, handPos[i].transform.position.y - yOffset, handPos[i].transform.position.z - zOffset), Quaternion.identity, handPos[i].transform);
newCard.name = card;
if (card == hands[i][hands[i].Count - 1])
{
newCard.GetComponent<Selectable>().faceUp = true;
}
yOffset = yOffset + 0.3f;
zOffset = zOffset + 0.03f;
discardPile.Add(card);
}
}
foreach (string card in discardPile)
{
if (deck.Contains(card))
{
deck.Remove(card);
}
}
discardPile.Clear();
}
void SortHand()
{
for (int i = 0; i < 1; i++)
{
for (int j = i; j < 1; j++)
{
hands[j].Add(deck.Last<string>());
//deck.RemoveAt(deck.Count - 1);
}
}
}
public void SortDeck()
{
//to draw 3
draw = deck.Count / 3;
drawRemainder = deck.Count % 3;
deckDraw.Clear();
int modifier = 0;
for (int i = 0; i < draw; i++)
{
List<string> myDraw = new List<string>();
for (int j = 0; j < 3; j++)
{
myDraw.Add(deck[j + modifier]);
}
deckDraw.Add(myDraw);
modifier = modifier + 3;
}
if (drawRemainder != 0)
{
List<string> myRemainders = new List<string>();
modifier = 0;
for (int k = 0; k < drawRemainder; k++)
{
myRemainders.Add(deck[deck.Count - drawRemainder + modifier]);
modifier++;
}
deckDraw.Add(myRemainders);
draw++;
}
deckLocation = 0;
}
public void DealFromDeck()
{
// add remaining cards to discard pile
foreach (Transform child in deckButton.transform)
{
if (child.CompareTag("Card"))
{
deck.Remove(child.name);
discardPile.Add(child.name);
Destroy(child.gameObject);
}
}
if (deckLocation < draw)
{
// draw card
drawOnDisplay.Clear();
float xOffset = 2.0f;
float zOffset = 0f;
foreach (string card in deckDraw[deckLocation])
{
GameObject newTopCard = Instantiate(cardPrefab, new Vector3(deckButton.transform.position.x + xOffset, deckButton.transform.position.y, deckButton.transform.position.z + zOffset), Quaternion.Euler(0, 0, Random.Range(0.0F, 1.0F) < 0.5F ? 0.0F : -180.0F), deckButton.transform);
xOffset = xOffset + 1.5f;
//zOffset = zOffset - 0.2f;
newTopCard.name = card;
drawOnDisplay.Add(card);
newTopCard.GetComponent<Selectable>().faceUp = true;
}
deckLocation++;
}
else
{
//Restack the top deck
RestackTopDeck();
}
}
void RestackTopDeck()
{
foreach (string card in discardPile)
{
deck.Add(card);
}
discardPile.Clear();
SortDeck();
}
}
public class UpdateSprite : MonoBehaviour
{
public Sprite cardFace;
public Sprite cardBack;
private SpriteRenderer spriteRenderer;
private Renderer cardRenderer;
private Selectable selectable;
private Gameplay gameplay;
// Start is called before the first frame update
void Start()
{
List<string> deck = Gameplay.GenerateDeck();
gameplay = FindObjectOfType<Gameplay>();
int i = 0;
foreach (string card in deck)
{
if (this.name == card)
{
cardFace = gameplay.cardFaces[i];
break;
}
i++;
}
spriteRenderer = GetComponent<SpriteRenderer>();
cardRenderer = GetComponent<Renderer>();
selectable = GetComponent<Selectable>();
}
// Update is called once per frame
void Update()
{
if (selectable.faceUp == true)
{
spriteRenderer.sprite = cardFace;
}
else
{
spriteRenderer.sprite = cardBack;
}
}
}
Your answer

Follow this Question
Related Questions
How do i change a sprite when another gameobject with the same prefab is colliding / is near 1 Answer
How do i change a sprite when another gameobject with the same prefab is colliding / is near 1 Answer
Changing the alpha channel of a sprite using the new Unity sprite system 1 Answer
What is the best way to instatiate an array of connected GameObjects? 0 Answers
Transport unknown amout of objects with GameObject array 0 Answers