How to make this a LOOP, rather than 5 IF's.

Hi all,

So this code here works just fine. I’m making a blackjack game, and this is how I handle my hits. Basically every time you push the hit button, I disable it and replace it with another hit button, so that I can tell it what to name, and where to put the next card. But it seems to me that I should be able to do this with a loop of some sort. Can anyone help me out? As I said, the code works exactly like I want it to. I just want to know a better more efficient way of doing it, just for my own education.

Thanks,

Adam

if(playerHit1)
	{
		if(GUI.Button(Rect (Screen.width * 0.5 - iconSize.width * 0.25, Screen.height * 0.9 - iconSize.height * 0.25, iconSize.width/2, iconSize.height/2), "", "hit"))
		{
			var hitCard1 : GameObject = DealCard();
			hitCard1.transform.position = posHit1.transform.position;
			hitCard1.name = "hit1";
			playerHitCard1 = GameObject.Find("hit1");
			var hit1 = playerHitCard1.GetComponent(WhatCard).cardNumber;
			if(hit1 == 11)
			{
				playerAces += 1;
				playersHand += 11;
			}
			else
			{
				playersHand += hit1;
			}
			
			playerHit1 = false;
			playerHit2 = true;
			
			UpdatePlayerScore();
		}
	}
	
	if(playerHit2)
	{
		if(GUI.Button(Rect (Screen.width * 0.5 - iconSize.width * 0.25, Screen.height * 0.9 - iconSize.height * 0.25, iconSize.width/2, iconSize.height/2), "", "hit"))
		{
			var hitCard2 : GameObject = DealCard();
			hitCard2.transform.position = posHit2.transform.position;
			hitCard2.name = "hit2";
			playerHitCard2 = GameObject.Find("hit2");
			var hit2 = playerHitCard2.GetComponent(WhatCard).cardNumber;
			if(hit2 == 11)
			{
				playerAces += 1;
				playersHand += 11;
			}
			else
			{
				playersHand += hit2;
			}
			
			playerHit2 = false;
			playerHit3 = true;
			
			UpdatePlayerScore();
		}
	}
	
	if(playerHit3)
	{
		if(GUI.Button(Rect (Screen.width * 0.5 - iconSize.width * 0.25, Screen.height * 0.9 - iconSize.height * 0.25, iconSize.width/2, iconSize.height/2), "", "hit"))
		{
			var hitCard3 : GameObject = DealCard();
			hitCard3.transform.position = posHit3.transform.position;
			hitCard3.name = "hit3";
			playerHitCard3 = GameObject.Find("hit3");
			var hit3 = playerHitCard3.GetComponent(WhatCard).cardNumber;
			if(hit3 == 11)
			{
				playerAces += 1;
				playersHand += 11;
			}
			else
			{
				playersHand += hit3;
			}
			
			playerHit3 = false;
			playerHit4 = true;
			
			UpdatePlayerScore();
		}
	}
	
	if(playerHit4)
	{
		if(GUI.Button(Rect (Screen.width * 0.5 - iconSize.width * 0.25, Screen.height * 0.9 - iconSize.height * 0.25, iconSize.width/2, iconSize.height/2), "", "hit"))
		{
			var hitCard4 : GameObject = DealCard();
			hitCard4.transform.position = posHit4.transform.position;
			hitCard4.name = "hit4";
			playerHitCard4 = GameObject.Find("hit4");
			var hit4 = playerHitCard4.GetComponent(WhatCard).cardNumber;
			if(hit4 == 11)
			{
				playerAces += 1;
				playersHand += 11;
			}
			else
			{
				playersHand += hit4;
			}
			
			playerHit4 = false;
			playerHit5 = true;
			
			UpdatePlayerScore();
		}
	}
	
	if(playerHit5)
	{
		if(GUI.Button(Rect (Screen.width * 0.5 - iconSize.width * 0.25, Screen.height * 0.9 - iconSize.height * 0.25, iconSize.width/2, iconSize.height/2), "", "hit"))
		{
			var hitCard5 : GameObject = DealCard();
			hitCard5.transform.position = posHit5.transform.position;
			hitCard5.name = "hit5";
			playerHitCard5 = GameObject.Find("hit5");
			var hit5 = playerHitCard5.GetComponent(WhatCard).cardNumber;
			if(hit5 == 11)
			{
				playerAces += 1;
				playersHand += 11;
			}
			else
			{
				playersHand += hit5;
			}
			
			playerHit5 = false;
			
			UpdatePlayerScore();
		}
	}
Transform[] hitPositions;
int hitIndex = 0;

if (GUI.Button(...))
{
    GameObject hitCard = DealCard();
    hitCard.transform.position = hitPositions[hitIndex].position;
    hitCard.name "hit" + (hitIndex + 1);
    // your GameObject.Find is redundant because hitCard already holds a reference to that GameObject
    int hit = hitCard.GetComponent<WhatCard>().cardNumber;
   if (hit == 11)
   // on and on

    hitIndex++;  
}

Ah! That makes sense. Thank you, sir! I don’t have a lot experience using arrays yet, thanks for helping me to see it.