Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Lodos3 · May 15, 2012 at 10:10 PM · c#cardshuffle

Random Tag Call Script [C#]

I'm looking for a way to randomly call 0 to 51 Tags (Prefabs that i have Tagged from 0 to 51). These prefabs are Card textures (clubs,spades,hearts and diamonds) and i want a button that you can click to randomly call a card.... I also need to have cards that are called to be removed from the list, so they don't come up twice in one game... (I'l be adding a RESET button).

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Drakestar · May 16, 2012 at 06:24 AM

 private List<Card> deck = new List<Card>();

 ... add your cards to the list ...

 Card GetCard()
 {
     int idx = Random.Range(0, deck.Count);
     Card theCard = deck[idx];
     deck.RemoveAt(idx);
     return theCard;
 }

 void OnGUI()
 {
 ... call GetCard() here ...
 }
Comment
Add comment · Show 2 · 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 syclamoth · May 16, 2012 at 06:54 AM 0
Share

I'd have used a shuffle method on my list, and then drawn from the top... but this is simple (even if it doesn't have the elegance of working the exact same way a real deck of cards does).

avatar image Lodos3 · May 17, 2012 at 01:19 PM 0
Share

I've attached all my card prefabs to an empty gameobject in the Hierarchy called "Cards". Heres my script im using to attach the prefabs..

public Transform Club1;

public Transform Club2;

public Transform Club3;

public Transform Club4;

public Transform Club5;

public Transform Club6;

public Transform Club7;

public Transform Club8;

public Transform Club9;

public Transform Club10;

public Transform Club11;

public Transform Club12;

public Transform Club13;

public Transform Heart1;

public Transform Heart2;

public Transform Heart3;

public Transform Heart4;

public Transform Heart5;

public Transform Heart6;

public Transform Heart7;

public Transform Heart8;

public Transform Heart9;

public Transform Heart10;

public Transform Heart11;

public Transform Heart12;

public Transform Heart13;

public Transform Spade1;

public Transform Spade2;

public Transform Spade3;

public Transform Spade4;

public Transform Spade5;

public Transform Spade6;

public Transform Spade7;

public Transform Spade8;

public Transform Spade9;

public Transform Spade10;

public Transform Spade11;

public Transform Spade12;

public Transform Spade13;

public Transform Diamond1;

public Transform Diamond2;

public Transform Diamond3;

public Transform Diamond4;

public Transform Diamond5;

public Transform Diamond6;

public Transform Diamond7;

public Transform Diamond8;

public Transform Diamond9;

public Transform Diamond10;

public Transform Diamond11;

public Transform Diamond12;

public Transform Diamond13;

How would i go by adding these to a list ? so that the above method works? please could someone dumb this down for me.

avatar image
0

Answer by Drakestar · May 16, 2012 at 01:46 PM

Syclamoth has a good point in the comments. For reference, here's a generic shuffle algorithm for a C# list that I've used for a while:

  // From http://stackoverflow.com/questions/273313/randomize-a-listt-in-c
     public static void Shuffle<T>(this IList<T> list)
         {
         Random rng = new Random();
         int n = list.Count;
         while (n > 1)
             {
             n--;
             int k = rng.Next(n + 1);
             T value = list[k];
             list[k] = list[n];
             list[n] = value;
             }
         }
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 Lodos3 · May 16, 2012 at 10:55 PM

Thanks guys for the replys , i didn't realise my post actually made it. i started a new one here ....

http://answers.unity3d.com/questions/253023/card-game-c.html#answer-253292

@Matthias , i've seen similar scripts "Fisher Yates" n such ... but i am a complete noob, this is my first EVER project and im confused on how to set my card prefabs (textures) to run with this posted scripts.

Comment
Add comment · Show 1 · 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 Drakestar · May 17, 2012 at 12:30 AM 0
Share

Once added to your project (which it should be if you add the ./ file to the Plugins folder) you can simply call $$anonymous$$yList.Shuffle() on any IList in C#. With the disclaimer that I haven't actually tried using this particular piece of code in Unity yet.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Cards: Stacking, drawing, hand, and shuffling 1 Answer

Stack Card 0 Answers

Flip over an object (smooth transition) 3 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