Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by Hathakas · Feb 07, 2015 at 08:58 PM · randomization

Is it possible to choose a random case (Switch)

Hey guys, I'm wondering if it's possible to choose a case randomly.

Like right now I have a drop down menu to select which cases I want. I want to add a button or something to randomize the cases. Kinda like a character creator with a randomize button.

Thanks!

Comment
Add comment · Show 3
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 sysameca · Feb 07, 2015 at 09:07 PM 0
Share

What kind of switch statement you have?

avatar image cdrandin · Feb 07, 2015 at 09:21 PM 0
Share

Depending on the type of your cases, it depends on how you would do it. If you are using Enum, C# has some ways in getting that info and you can put them in a list and randomize a number between 0 and the length - 1.

avatar image Hathakas · Feb 07, 2015 at 09:38 PM 0
Share

Oh sorry haha I guess I did not realize there's more than one switch statements. I'm using Enums.

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Bonfire-Boy · Feb 07, 2015 at 09:45 PM

You need to randomise the switch variable first.

Setting a random case of an Enum is a bit more complicated than an int. You need something like

 System.Array enumValues = System.Enum.GetValues(typeof(YourEnum));
 YourEnum randomEnum = (YourEnum)enumValues.GetValue(UnityEngine.Random.Range(0,enumValues.Length));
 switch (randomEnum)
 {
 }

Line 1 gives you an Array containing one of each of the values of your 'YourEnum' Enum.

Line 2 randomly picks an element of that array.

Comment
Add comment · Show 3 · 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 Hathakas · Feb 08, 2015 at 02:01 AM 0
Share

Thanks Again!

avatar image hms0589 · Oct 25, 2020 at 09:16 AM 0
Share

for add, you have to insert this code into awake() or start()

avatar image Bonfire-Boy hms0589 · Oct 25, 2020 at 04:25 PM 1
Share

No, you don't.

The code I've shown will pick a random enum and then perform a switch over it, which is what was asked for.

You might want to do that in Awake() or Start(), if you're using it to initialise something.

But there are many other times one might want to randomise something including in response to a request to do it (which is what the OP actually asked for).

avatar image
0

Answer by Mmmpies · Feb 07, 2015 at 09:25 PM

Yep Random.Range can return an int (or float) just set it to start from the start range to an end range.

It works slightly differently for Int and Float. with Float the upper range is inclusive, with Int it's exclusive.

So if you have 4 options you want to use randomly use this:

 int MyRandom = Random.Range (1, 5); // between 1 and 4

but floats are inclusive so 1 to 5 would work for all floats.

for what you want I think int Random.Range(1, X); will work where X is the upper value.

Comment
Add comment · Show 8 · 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 Hathakas · Feb 07, 2015 at 09:39 PM 0
Share

Sorry I'm quite new to program$$anonymous$$g, so that answers the Random generator part. How would I go about making the switch statement be affected by the $$anonymous$$yRandom number?

avatar image Hathakas · Feb 07, 2015 at 10:11 PM 0
Share

Ok that definitely looks complicated for me. I take it I need to declare an Array before this code? When I insert it anywhere in my script, the Array is red, kinda like when it can't find the ref to a variable.

avatar image Bonfire-Boy · Feb 07, 2015 at 10:22 PM 1
Share

Sorry, my bad, I left some pseudo in there. Have fixed it now. Array should have been YourEnum[] (an array of your enums). The GetValues function sets it so that it contains one of each possible value of that enum, and then a random case is picked from that array.

avatar image Bonfire-Boy · Feb 07, 2015 at 10:39 PM 1
Share

$$anonymous$$y double bad, I think you just didn't have the System namespace loaded. Have fixed it again adding in explicit namespaces.

avatar image Hathakas · Feb 07, 2015 at 10:39 PM 0
Share

Haha, that's a relief! Ok so I put my enum in the but now the Enum.GetValues is red. everything else seems ok. I put all of them in Start() for now should I not have done that?

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

23 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

Related Questions

Random Generation Algorithm Error 3 Answers

How do I randomize my Music played? The opening song is always the same song and I don't know why. 1 Answer

How do I store and access multiple randomly generated characters? 0 Answers

Polybrush prefab rotation increment 0 Answers

How to activate a script from another object? 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