# integer deleting himself

Hello, I am trying to make BlackJack game but I have one problem where I want to call a method to generate random card and then add it to existing integer (Player Number or Dealer Number) but for some reason, method reset existing number on integer (Player Number or Dealer Number) in the end of method.

This is just method for a button to start a game:

``````public void StartGame()
{
RandomCard(DealerNumber, AceDealer);
RandomCard(PlayerNumber, AcePlayer);
RandomCard(PlayerNumber, AcePlayer);
}
``````

This is method for generating random card:

`````` public void RandomCard(int person, int ace)
{
randomcard = Random.Range(2, 15);
if (randomcard == 11) //Ace
{
if (Ace > 2)
{
randomcard = Random.Range(2, 11);
}
else { Ace++; ace++; }
}
if(randomcard >= 12) //K, Q, J, 10
{
randomcard = 10;
}
person += randomcard;

}
``````

I have really no idea what causes the problem. Thank you for any advice

I tried putting print everywhere, here’s the result:

``````public void StartGame()
{
RandomCard(DealerNumber, AceDealer);
print(DealerNumber + " mid");
RandomCard(PlayerNumber, AcePlayer);
print(PlayerNumber + " mid");
RandomCard(PlayerNumber, AcePlayer);
}
``````
``````public void RandomCard(int person, int ace)
{
print(person + " before");
randomcard = Random.Range(2, 15);
if (randomcard == 11) //Ace
{
if (Ace > 2)
{
randomcard = Random.Range(2, 11);
}
else { Ace++; ace++; }
}
if(randomcard >= 12) //K, Q, J, 10
{
randomcard = 10;
}
person += randomcard;
print(person + " after");
}
``````

Output:
0 before
2 after
0 mid
0 before
10 after
0 mid
0 before
10 after

Value types (including all the primitives, int float bool string and so on) are passed by value, which is to say that when you call RandomCard(x, y), RandomCard’s “person” is a copy of x, but does not refer to it. So when you change the value of “person”, that change is not persisting anywhere - you’re modifying person, but not modifying x.

You can use the “ref” keyword, and it will modify the original variable passed into it:

``````public void RandomCard(ref int person, int ace) {
....
}

//when calling it
RandomCard(ref x, y);
``````