I'm trying to build a simple little game/program that looks at two hands of cards and compares them on multiple levels. I've created 5 prefabs for player 1 and 5 for player 2. Their names are Player 1 Card 1 through Player 2 Card 5.
I've also got all the sprites loading into an array. The array of strings below it are the exact names, order, and capitalization of each of the cards in the deck.
What I want to do is basically tell the game to change these spriteless prefabs during start to the cards that were chosen. The problem with the current system is that if it deletes item #3 on the list and shortens the array by 1, the next time it rolls (if it rolls 3 again) it'll still pick that same image. The only thing being reduced are the cards at the end of the array.
What I'd like to do is something like this:
GameObject.FindGameObjectWithTag ("Player 2 Card " + _p2).GetComponent ().sprite = CardNumberToDraw as Sprite;
That way it pulls the entry at that line so that if it hits the same value 2-10 times it'll still be different cards each time.
I'm utterly stumped at this point. Entirety of code is as follows and works perfectly fine with index integers:
PS. I'm aware it is messy. I usually play around until things work and then clean up the code. That might be a poor idea.
public class GameController : MonoBehaviour {
// Use this for initialization
void Start ()
{
/*
* This loads all the art assetts we'll be using for the cards!
* */
Sprite[] CardSprites = Resources.LoadAll<Sprite> ("Card Sprites");
/*
* This is an array containing all the values of cards in poker.
* */
string[] TheNewDeck = new string[52] {
"2c", "2d", "2h", "2s", "3c", "3d", "3h", "3s", "4c","4d", "4h",
"4s", "5c", "5d", "5h", "5s", "6c", "6d", "6h", "6s", "7c", "7d",
"7h", "7s", "8c", "8d", "8h", "8s", "9c", "9d", "9h", "9s", "ac",
"ad", "ah", "as", "jc", "jd", "jh", "js", "kc", "kd", "kh", "ks",
"qc", "qd", "qh", "qs", "tc", "td", "th", "ts"
};
/*
* We'll use this deck so we can break it all we want and then refill it with the contents of the original.
* */
var TheDealingDeck = new ArrayList ();
for (int _i = 0; _i < TheNewDeck.Length; _i++) {
TheDealingDeck.Add (TheNewDeck [_i]);
//TheDealingDeck.Add (_i);
}
/*
* The next thing we need to do is create 2 arrays. One for player 1 and one for player 2.
* These are their hands.
* */
var PlayerOneHand = new ArrayList ();
var PlayerTwoHand = new ArrayList ();
/*
* Here we deal out a hand to the player.
* _p1 starts at one because the 1st card in your hand is Player 1 Card 1. Ends at Player 1 Card 5.
* */
for (int _p1 = 1; _p1 < 6; _p1++) {
var RandomRange = Random.Range (0, TheDealingDeck.Count - 1);
var CardNumberToDraw = System.Convert.ToInt16 (TheDealingDeck [RandomRange]);
GameObject.FindGameObjectWithTag ("Player 1 Card " + _p1).GetComponent<SpriteRenderer> ().sprite = CardSprites [CardNumberToDraw];
PlayerOneHand.Add (TheNewDeck [CardNumberToDraw]);
TheDealingDeck.RemoveAt (RandomRange);
}
for (int _p2 = 1; _p2 < 6; _p2++) {
var RandomRange = Random.Range (0, TheDealingDeck.Count - 1);
var CardNumberToDraw = System.Convert.ToInt16 (TheDealingDeck [RandomRange]);
GameObject.FindGameObjectWithTag ("Player 2 Card " + _p2).GetComponent<SpriteRenderer> ().sprite = CardSprites [CardNumberToDraw];
PlayerTwoHand.Add (TheNewDeck [CardNumberToDraw]);
TheDealingDeck.RemoveAt (RandomRange);
}
}
// Update is called once per frame
void Update ()
{
if (Input.GetKeyUp (KeyCode.Space)) {
Application.LoadLevel (Application.loadedLevelName);
}
}
}
PlayerOneHand.Add (TheDealingDeck [_p1]);use the card you chose fromTheDealingDeck? You're adding _p1 but the card you chose isRandom.Range (0, TheDealingDeck.Count), same with the next lineTheDealingDeck.RemoveAt(_p1)- Jerdak