Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by IGOR824 · Feb 03, 2020 at 04:10 PM · gameobjectarraysprite

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;
         }
     }

 }


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

221 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges