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 /
  • Help Room /
avatar image
3
Question by Lars_Hulsmans · May 20, 2016 at 02:12 PM · randomarraysordercardsshuffle

I'm trying to shuffle an array's order

So i'm trying to make a card game and i want to shuffle the deck but i have no idea how to get stuff from one array to an other so i really hope someone can help me. this is the script i want to use to shuffle the cards. using UnityEngine; using System.Collections;

 public class DeckScript : MonoBehaviour {
     //the deck 60 cards
     [SerializeField]
     GameObject[] decklist;
 
     //shuffle arrays 20 cards each 
     [SerializeField]
     GameObject[] shuffleArray1;
     [SerializeField]
     GameObject[] shuffleArray2;
     [SerializeField]
     GameObject[] shuffleArray3;
 
     private int arraystate = 1;
     // Use this for initialization
     void Start () {
         Shuffle();
     }
     
     // Update is called once per frame
     void Update () {
     
     }
     void Shuffle()
     {
         int randomnumber = Random.Range(4, 20);
         for (int a = 0; a < randomnumber; a++)
         {
             for (int i = 0; i < decklist.Length; i++)
             {
                 switch (arraystate)
                 {
                     case 1:
                         //take card from decklist put in shufflearray1
                         Debug.Log("shuffleArray1");
                         arraystate += 1;
                         break;
                     case 2:
                         //take card from decklist put in shufflearray2
                         Debug.Log("shuffleArray2");
                         arraystate += 1;
                         break;
                     case 3:
                         //take card from decklist put in shufflearray3
                         Debug.Log("shuffleArray3");
                         arraystate += 1;
                         break;
                 }
                 if (arraystate > 3)
                 {
                     arraystate = 1;
                 }
             }
             //dump all shufflearrays in deckllist in random order
         }
     }
 }
 
Comment
Add comment · Show 1
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 HarshadK · May 20, 2016 at 02:32 PM 0
Share

Here is a script to shuffle deck of cards: Deck of Cards

3 Replies

· Add your reply
  • Sort: 
avatar image
9
Best Answer

Answer by antx · May 20, 2016 at 02:32 PM

This should shuffle the gameObjects in decklist[] without the need of a 2nd array.

(decklist[] should be filled first in the inspector or from another script):

 using UnityEngine;
 using System.Collections;
 
 public class DeckShuffle : MonoBehaviour {
     public GameObject[] decklist;
     private GameObject tempGO;
     
     void Start () {
         Shuffle();
     }
     
     public void Shuffle() {
         for (int i = 0; i < decklist.Length; i++) {
             int rnd = Random.Range(0, decklist.Length);
             tempGO = decklist[rnd];
             decklist[rnd] = decklist[i];
             decklist[i] = tempGO;
         }
     }
 }
 
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
6

Answer by Louis-N-D · Mar 15, 2020 at 10:25 PM

The accepted answer from antx isn't bad but it will produce weighted results. I know because it's the same solution I came up with when considering the same problem and I was corrected by a more experienced programmer at work.

Two small changes need to be made to fix the imbalance. 1. Stop the loop before the last entry. 2. Randomize from i to Length rather than 0 to Length.

The modified function should look like this:

 public void Shuffle() 
 {
          for (int i = 0; i < decklist.Length - 1; i++) 
          {
              int rnd = Random.Range(i, decklist.Length);
              tempGO = decklist[rnd];
              decklist[rnd] = decklist[i];
              decklist[i] = tempGO;
          }
 }

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 farhanblu · May 20, 2016 at 07:05 PM

The question has been answered quite elegantly here . Basically, just make an extension method for IEnumerables and you are good to go. Just call decklist.Shuffle() and done.

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 Lars_Hulsmans · May 20, 2016 at 08:28 PM 0
Share

thank you all so much

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

7 People are following this question.

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

Related Questions

Question about "shuffling" a deck of cards 2 Answers

List or Arrays, and how to randomize them. 1 Answer

null array? 0 Answers

Use all variables from array (in Random.Range()) 2 Answers

create a function, return random gameObject from an array? 1 Answer


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