- Home /
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).
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 ...
}
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).
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.
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;
}
}
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.
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
Follow this Question
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