Is it possible to check the value of Random.Range with an OnClick function?

So I have a scenario where there is a 50/50 chance the user will be correct. I have a script that creates the random number and then I want to check that via an OnClick function. I’m having trouble explaining it so I’ll just write it out.

case ("50% chance");
//50% chance
            Random rand4 = new Random();
            int chance4 = Random.Range(1, 3);
            print("Your number is" + chance4);
            break;

so here’s one of the scenarios in a switch where there’s a 50% chance to be right or wrong. currently the chance prints out correctly, 1 or 2. I then want to check if the number is 1 and the player clicks The wrong button (Yes, or no to continue) then they lose, otherwise they win.

So to go into it further i’d say.

print(Your number is 1);

then I want to either in this script or another script have a

public void OnClickYes()
{
    print("Your number is" + chance4);
    if(chance4 == 1)
    GameOver(); //or something like this

    if (chance4 == 2)
    YouWin();
}

what’s happening right now is that chance4 in the OnClickYes() function always returns 0. So I don’t know why that is. I don’t really want to run the switch statement in both functions though because it seems like bad coding. Am I missing something easy?

I assume based on the way this is laid out that you have a member field declared for chance4.

But you are declaring a new local variable chance4 in your switch case by declaring ‘int chance4’, then assigning to it, and never using it so that value is lost. Remove the ‘int’ definition before the chance4 there so it uses it correctly.

Edit:
also, while I don’t know the rest of your code, the case line should probably end in a colon ‘:’ not a semicolon.