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");
}
}
}
}
}