Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Sp33dy_Sn1pa · Oct 24, 2014 at 09:12 PM · c#randompercentagelevelload

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");
                 }
             }
         }
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sp33dy_Sn1pa · Oct 24, 2014 at 09:53 PM 0
Share

& 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.

avatar image bubzy · Oct 24, 2014 at 09:56 PM 0
Share

oh wait. you are making a new random value everytime here :/

let me edit my answer a sec

avatar image Sp33dy_Sn1pa · Oct 24, 2014 at 10:05 PM 0
Share

Okay, that's more like it. I'll try that now.

avatar image Sp33dy_Sn1pa · Oct 24, 2014 at 10:32 PM 0
Share

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

avatar image Sp33dy_Sn1pa · Oct 24, 2014 at 10:43 PM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges