- Home /
Trouble with Random.value and Percentage
Hello all and thank you for taking a look at my issue. I'm trying to put some finishing touches on a Level Loading script I made. The idea behind the script is that there are 7 levels to be loaded however, the first 6 each have a 16.5 percent chance of being selected when the GUI button is pressed leaving only a 1 percent for the 7th and final level to be selected. At first glance the code seemed to work as expected, but then I added Debug.Log to send a message letting me know which level was being selected and that's when I noticed the anomaly. Occasionally when the GUI button is pressed, the console will show more than one level is being selected even though only one of the levels is running. If anyone has any suggestions as to a solution I would greatly appreciate the help. Here is my code.
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
public class GUI_RandomLevelLoader1 : MonoBehaviour
{
public bool debugMode = false;
public bool centerButton = false;
public Rect button = new Rect(15, 15, 200, 110);
public string buttonLabel = "Load Level";
public GUISkin skin = null;
public void OnGUI()
{
if (debugMode || Application.isPlaying)
{
if (centerButton)
{
button.x = (Screen.width * 0.5f) - (button.width * 0.5f);
}
GUI.skin = skin;
GUI.Label(button, buttonLabel);
if (GUI.RepeatButton(button, buttonLabel))
{
if(Random.value >= 0.835) //16.5% Chance
{
Application.LoadLevel(2);
Debug.Log("Black");
}
if(Random.value <= 0.835 & Random.value >= 0.67) //16.5% Chance
{
Application.LoadLevel(3);
Debug.Log("Green");
}
if(Random.value <= 0.67 & Random.value >= 0.505) //16.5% Chance
{
Application.LoadLevel(4);
Debug.Log("Blue");
}
if(Random.value <= 0.505 & Random.value >= 0.34) //16.5% Chance
{
Application.LoadLevel(5);
Debug.Log("Orange");
}
if(Random.value <= 0.34 & Random.value >= 0.175) //16.5% Chance
{
Application.LoadLevel(6);
Debug.Log("Pink");
}
if(Random.value <= 0.175 & Random.value >= 0.01) //16.5% Chance
{
Application.LoadLevel(7);
Debug.Log("Red");
}
if(Random.value <= 0.01) //1% Chance
{
Application.LoadLevel(8);
Debug.Log("Gold");
}
}
}
}
}
Answer by bubzy · Oct 24, 2014 at 09:13 PM
//old answer :p
the syntax for "and" is
&&
not
&
//old answer
you need to do something like
float range = Random.value;
then reference range
in your if's
& is a bitwise comparison which verifies both operands
&& is a logical comparison which stops evaluating if the first operand is false
Unfortunately, in this particular instance, there is no noticeable change in behavior with the addition of the second & symbol. Thanks for the suggestion though.
oh wait. you are making a new random value everytime here :/
let me edit my answer a sec
So here is the error that I've run into:
ArgumentException: get_value can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene. Don't use this function in the constructor or field initializers, ins$$anonymous$$d move initialization code to the Awake or Start function. GUI_RandomLevelLoader1..ctor ()
However, when I move it to Awake or Start I get the following error for each instance of "range":
Assets/Scripts/GUI_RandomLevelLoader1.cs(47,36): error CS0103: The name `range' does not exist in the current context
Well, I solved one of my issues. I changed GUI.Button to GUI.RepeatButton so now when the button is pressed it loads a level every time. The only issue left is trying to figure out why multiple levels load simultaneously.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
probability of child Game objects 1 Answer
How to stop MoveTowards teleporting 1 Answer
Random Children Array that changes Parent, not working right 0 Answers