Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 SuperMeta-Studios · Apr 09, 2018 at 02:41 PM · scripting problemscripting beginner

Random Select (Scripting Problem)

So Once again noob question. SO what I'm trying to do is to generate a random number between 0 - 5 and use it in the "if" statement to call functions.

 void Update ()
 {
     if (blockSelected == 0)
     {
         FunctionOne();
     }
     if (blockSelected == 1)
     {
         FunctionTwo();
     }
     .
     .
     .
     if (blockSelected == 4)
     {
         FunctionFive();
     }
 }

Now I want to randomly select a function every time the Player hits the trigger. Can you guys please help how to do this?

Comment
Add comment · Show 1
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 TreyH · Apr 09, 2018 at 02:49 PM 0
Share

you have everything there already, maybe what you're looking for is Random.Range?

 int rand = Random.Range(0, 5+1);

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Topthink · Apr 09, 2018 at 02:51 PM

Consider:

 int tempInteger = Random.Range(0,6);

That will give you a number between 0 and 5 inclusive...the high number will be excluded. After this, you might include your "If" statements (Look at "switch" statements, might be more effective). See here: https://unity3d.com/learn/tutorials/topics/scripting/switch-statements

It sounds like you know how to use "if" statements. If you choose to use them, you would follow your Random above with your "if".

if (tempInteger==0) {...then do something here, call a function etc....}

if (tempInteger==1) {...then do something here, call a function etc....}

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 SuperMeta-Studios · Apr 09, 2018 at 03:13 PM 0
Share

Hello Thank you very much. Follow up question. Is there a way to make sure the same number do not repeat until all the numbers in the range is used at least once?

avatar image Topthink SuperMeta-Studios · Apr 09, 2018 at 03:29 PM 1
Share

Sure, there is a way to do almost anything. I did something similar in a project I'm working on now...I was selecting several objects from a list of several dozen and I didn't want to select the same item more than once. I used custom classes but it might be better to use a simple two dimensional array. There would be a lot of detail that goes with this explanation but here is a "High Level" explanation of one way to accomplish this. Use an Array that is size 6x2: put all your numbers (0-5) in one column and then put a zero (or whatever) in the second column. When you select a random number, change the zero in the second column to a "1" or something. Then, when you select the second "random" number, check the corresponding array column to see if it is already been selected (it would be a "1" or whatever). If it has been selected, then you would select another number (use something to loop it...many, many ways to loop). This would ensure that you select each number once before you select anything more than once. Again, there are so many ways to accomplish this...this would be just one of dozens, hundreds, etc. If you really need it, I could post up some of my code as an example, but at the point you are now, it might not mean much. Best wishes however.

avatar image SuperMeta-Studios Topthink · Apr 09, 2018 at 06:04 PM 0
Share

Thank yo very much. Let me try and get back to you.

Show more comments
avatar image Harinezumi SuperMeta-Studios · Apr 09, 2018 at 03:34 PM 1
Share

Having values in a random order is called "shuffling" (just like in playlists). Here is a good implementation: https://answers.unity.com/questions/16531/randomizing-arrays.html

avatar image TreyH SuperMeta-Studios · Apr 09, 2018 at 03:36 PM 1
Share

These forums aren't intended to field questions about common program$$anonymous$$g encounters. Random number generation without duplicates is an exhausted topic on other forums and can easily be solved by Google: https://stackoverflow.com/questions/26931528/random-number-generator-with-no-duplicates

If this response answered your original question, then mark it as the solution so this question can be closed. If you have other basic C# questions in the future, please make sure no one else has already asked / solved a similar question.

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

143 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

First Person Controller - Problems with footstep audio being played too fast. 0 Answers

How to load a scene with GameFlow on button click ? ( XR RIG / Oculus Quest ) 0 Answers

How to pass color of two colliding gameobjects to each other? 2 Answers

On/Off Trigger is making Light Flicker 1 Answer

My camera script is having a lot of problems..... 2 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