question on the best way to create a percentage chance something happens

I am trying to work out how to make a button that is clicked on have a certain percentage chance to fail rather then just succeed on every click. what do you guys think the best way to put that into a C# script would be?

any ideas or tips/suggestions would be great, i am currently browsing some documentation now trying to work out a way to do it myself.

thanks!

Try this out.

float percentChance = 0.1f;
if (Random.value <= percentChance){
    Debug.Log("Success");
} else {
    Debug.Log("Fail");
}
1 Like

awesome, thank you for the response, i have not tried this yet, but it looks like it would be just what im looking for. will try it out later this afternoon and get back to you if i have any questions. thanks for the help!

works beautifully, thank you! did some tweaking to go with the script im working on. thanks again!