• 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 Hosky · Dec 22, 2016 at 06:30 PM · scripting problemspritesarrays

2D Sprite Trouble

Hey all, I'm making a simple playing card game and have been trying to fix this issue for the last 2 hours without success. It's kinda hard to fully show the problem but Ill try my best.

  • I spawn multiple card prefabs.

  • I create a variable name RandomCard and generate a random number between 0 and the count of my CardsInDeck list - 1.

  • I create a variable named DrawnCard
    and assign it CardsInDeck[RandomCard]

  • I then cycle through an array of
    sprites (the card faces) and check if sprite name is == DrawnCard If so I
    assign that sprite to the card.

  • Lastly, I remove
    CardsInDeck(DrawnCard).

However, the issue I'm having is that there are still double ups. All of the DrawnCard values are unique and there are no dupes. The array of sprites are all unique in name and appearance.

I hope this is clear enough to understand, if not please ask. Code Below:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class boardManager1 : MonoBehaviour {
     //Deck Makeup
     private List<string> CardsInDeck = new List<string>();
     private List<string> cardSuits = new List<string>() {
             "Clubs",
             "Spades",
             "Hearts",
             "Diamonds"
         };
 
     //Card Creation
     public Sprite[] cardImages;
     public GameObject Card;
     private int[] EmptyCardPositions = { -5, -3, -1, 1, 3, 5, 7 };
 
     void CreateDeck()
     {
         for (int cardsInDeck = 0; cardsInDeck <= 13; cardsInDeck++)
         {
             for (int cardsInSuit = 0; cardsInSuit < 4; cardsInSuit++)
             {
                 CardsInDeck.Add(cardsInDeck.ToString() + cardSuits[cardsInSuit]);
             }
         }
     }
     void DrawCard(int xPos, int yPos)
     {
         Instantiate(Card, new Vector3(xPos, yPos, -1), Quaternion.identity);
         int randomCard = Random.Range(0, CardsInDeck.Count-1);
         string DrawnCard = CardsInDeck[randomCard];
         Debug.Log(DrawnCard);
         foreach (var sprite in cardImages)
         {
             if (sprite.name == DrawnCard)
             { 
                 Card.GetComponent<SpriteRenderer>().sprite = sprite;
             }
         }
         //Remove Drawn card from the deck
         CardsInDeck.Remove(DrawnCard);
     }
 
     void Start()
     {
         CreateDeck();
         //Create Player Hand
         foreach (var position in EmptyCardPositions)
         {
             DrawCard(position,-4);
         }
         //Create AI Hand
         foreach (var position in EmptyCardPositions)
         {
             DrawCard(position, 2);
         }
     }
 
 }
 







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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by destructivArts · Dec 22, 2016 at 08:03 PM

I don't know why your code isn't working but you could just do:

 CardsInDeck.RemoveAt (randomCard);

which is a less expensive remove function anyways.

Hope this helps!

Comment
Add comment · Share
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
avatar image
0

Answer by scb · Dec 23, 2016 at 04:30 PM

Have you tried using the debugger, and tracing your functions step by step, all the way through? You should be able to keep an eye on the CardsInDeck list, and watch its' size and contents.

If you put a breakpoint on [line 44]

CardsInDeck.Remove(DrawnCard);

and watch the contents of your list, is it behaving?


Sorry, i know this is not technically an answer. Just an approach.

Comment
Add comment · Share
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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Array Declaration & Initialization- Inside and Outside Functions 2 Answers

Change sprite dinamically 1 Answer

A Sprite move like a racecar, can it be done? 1 Answer

Unity - Cut a sprite in 2 halfs using the form of another sprite 0 Answers

How can i create array of texture2d with variables names ? 2 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