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 noobunity · Oct 01, 2014 at 08:35 AM · random.rangerandomize

Putting answer randomly in options

Hi guys

I am generating random number of objects when game starts, and destroying them after some time. Then I am showing GUI with three options(3 numbers) and user has to select the correct one(i.e how many number of objects were there).

I don't know how to put the answer in one of the three options in random spots each time and then fill other two options with number close to the answer. (Note that objects are also getting generated randomly when game starts)

So basically what I want is if 4 objects are generated, then GUI will pop up with three option and within the three options, one will be the answer i.e 4 and other two options will be 5,6 or 3,5 etc.

Currently I am just hard coding the value as shown below.

 #pragma strict
 
 
 var respawn1:GameObject;
 var randomcars:int;
 var i:int;
 var showGUI = false;
 var myCam:Camera;
 
 function Start () {
 
 randomcars = Random.Range(1,6);
 
 for (var i : int = 1;i <= randomcars; i++) {
         Instantiate (respawn1, Vector3(i * 5, 0, 0), Quaternion.identity);
     }
 
 }
 
 function Update (){
 showGUI = (GameObject.FindWithTag("Cars") == null);
 
 }
 
 function OnGUI() {
 
 if (showGUI) {
 
 

GUI.Box (Rect (myCam.pixelWidth/2-75,myCam.pixelHeight/2-45,150,90), "How many cars?");

 if (GUI.Button (Rect (myCam.pixelWidth/2-40,myCam.pixelHeight/2-25,80,20), "1")) {
 
 if(randomcars==1){
         Application.LoadLevel ("Level2");
     }
 }
 
 if (GUI.Button (Rect (myCam.pixelWidth/2-40,myCam.pixelHeight/2,80,20), "2")) {
 
 if(randomcars==2){
         Application.LoadLevel ("Level2");
     }
 }
 
 
 
 if (GUI.Button (Rect (myCam.pixelWidth/2-40,myCam.pixelHeight/2+25,80,20), "3")) {
 
 if(randomcars==3){
         Application.LoadLevel ("Level2");
     }
 }
 
 }
 }
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
Best Answer

Answer by slavo · Oct 02, 2014 at 11:34 AM

Hi,

you need to write ranomize function that depends on what do you want to achive. Exapmle of one of many ways you can write this function is below. It is c# code, but it should not be problem to rewrite it to UnityScript.

     public List<int> GetRadnomAnswers(int correctAnswer, int numberOfAnswers, int dispersion)
     {
         //input check
         if (numberOfAnswers <= 0)
             return new List<int>(); //we do not want any answer
 
         List<int> randomAnswers = new List<int>();
 
         if (dispersion < 1)
         {
             randomAnswers.Add(correctAnswer);
             return randomAnswers; // no dispersion set, return only answer
         }
 
         if (dispersion == 1)
         {
             //we do not need randomnes
             randomAnswers.Add(correctAnswer - 1);
             randomAnswers.Add(correctAnswer);
             randomAnswers.Add(correctAnswer + 1);
 
             return randomAnswers;
         }
 
 
         //main loop
         int bottomLimit = Math.Max(1, correctAnswer - dispersion); //botom limit is inclusive
         int topLimit = correctAnswer + dispersion + 1; //top limit is exclusive
 
 
         while (randomAnswers.Count < numberOfAnswers)
         {
             int randomAnswer = Random.Range(bottomLimit, topLimit);
 
             if (randomAnswers.Contains(randomAnswer))
                 continue; //randomAnswer already in list
 
             randomAnswers.Add(randomAnswer);
         }
 
         //check if right answer is between random answers
         if (randomAnswers.Contains(correctAnswer) == false)
         {
             //it is not, we nedd to add it to random place
             int randomIndex = Random.Range(0, randomAnswers.Count + 1);
             randomAnswers[randomIndex] = correctAnswer;
         }
 
         return randomAnswers;
     }

You can call this function like this:

 List<int> randomAnswers = GetRadnomAnswers(randomcars, 3, 2);
 
         //your gui will work only for exactly 3 answers
         if (GUI.Button (Rect (myCam.pixelWidth/2-40,myCam.pixelHeight/2-25,80,20), randomAnswers[0].ToString())) {
             
             if(randomcars == randomAnswers[0]){
                 Application.LoadLevel ("Level2");
             }
         }
 
         if (GUI.Button (Rect (myCam.pixelWidth/2-40,myCam.pixelHeight/2,80,20), randomAnswers[1].ToString())) {
             
             if(randomcars == randomAnswers[1]){
                 Application.LoadLevel ("Level2");
             }
         }
 
         if (GUI.Button (Rect (myCam.pixelWidth/2-40,myCam.pixelHeight/2+25,80,20), randomAnswers[2].ToString())) {
             
             if(randomcars == randomAnswers[2]){
                 Application.LoadLevel ("Level2");
             }
         }


Comment
Add comment · Show 1 · 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 noobunity · Oct 03, 2014 at 01:05 AM 0
Share

Hi Slavo

Thanks, I will try to implement your solution in UnityScript

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

Randomize 2 clips from an AudioSource List by name? 1 Answer

Random.Range is not changing? 1 Answer

Hey! Im a beginner and need help in randomizing. (Im making a 2d game in Unity) 1 Answer

Are there better ways to create random behavior? 1 Answer

How to make Random.Range() step by a certain amount? 1 Answer


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