Greetings Everyone
How are you all today? good I hope =)
this is my first post here so ill try to explain the best i can.
here’s what I’m trying to do, I’m working on a monopoly game for my school’s final project and so far things are going smooth, but I’m having a little problem on the dice stytem
so far this is my code:
function Update () {
var dice1:int = Random.Range(1, 7);
var dice2:int = Random.Range(1, 7);
var diceResult = dice1 + dice2;
var playTimes:int = 0;
var playAgainTimes:int = 0;
var playAgain=false;
if(Input.GetButtonUp("Jump"))
{
playTimes = diceResult;
Debug.Log(playTimes);
if(dice1 == dice2){
playAgainTimes +=1;
Debug.Log("play again = " + playAgainTimes);
if(playAgainTimes == 3){
Debug.Log("Jail Time");
}else if(dice1 != dice2){
playAgainTimes=0;
Debug.Log("the number is " + playAgainTimes);
}
}
}
}
well so far i managed to do a random number generator and checking if the number of the dices where the same, and that would add a turn, if the turns where equal to 3 then the piece would go to jail, but the problem is, every time the dices are equal to each other more than 2 times , the var playAgainTimes is always equals to 1.
here’s a picture of it:
so what’s wrong?
I’m sorry but I’m still a little fresh on uniscript, i know a bit of javascript(still learning ^^)
I thank you all in advance for your time and thank you for helping me ^^
Well, your script is a little messy for your purpose: you will never roll twice in the same turn, nor go to jail with your script! The reason is in the variables' declaration inside the Update function: at every frame, playAgainTimes turns back to 0! In addition, I don't see a reason for playAgain variable, that's never used. To roll dice more times in the same turn, you have to instantiate variables outside Update function and declare a co-routine, calling this one if (dice1 == dice2).
My advice is: try to modify the script on your own, now, and come back if you'll encounter other problems!
P.S.: Also format your code properly, in the question, clicking on the "101010" button, so it will be more clear to all.
var playAgainTimes:int = 0;
var jailCheck:int=0;
var lock:int=0;
function Update() {
if(Input.GetButtonUp("Jump") and lock==0){
playAgainTimes=1;
jailCheck=1;
Rolling();
jailCheck=0;
}
}
function Rolling(){
lock=1;
while (var playAgainTimes>0){
var dice1:int = Random.Range(1, 7);
var dice2:int = Random.Range(1, 7);
var diceResult = dice1 + dice2;
Debug.Log("You obtain a " diceResult);
if(dice1 == dice2){
playAgainTimes +=1;
jailCheck+=1;
Debug.Log("Extra roll!");
}
if (jailCheck==3){
playAgainTimes=1;
jailCheck=0;
Debug.Log("You go to jail...");
}
playAgainTimes-=1;
}
lock=0;
}
Watch out!!! I can't be sure about its correctness, because I don't have Unity here, and I couldn't test that: it's just to point you in the right (for me, at least...) direction.
In the Update function, you simply check the spacebar pressure: in that case, I can roll once, and I launch Rolling function.
Here, until I have turns available, I:
Launch dice;
Check for an extra roll (+1 roll, in this case);
Check for jail (reset rolls, in this case);
Subtract a roll available (playAgainTimes-=1;)
Hope that this can help you; if you have any doubt (or if someone see mistakes here...) let me know!