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
         }
     }
 }
 
Here is a script to shuffle deck of cards: Deck of Cards
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;
         }
     }
 }
 
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;
          }
 }
Your answer
 
 
             Follow this Question
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
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                