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 Bormeth · Apr 04, 2011 at 09:11 PM · startenumstateleftright

enum choose Random :)

Got this

enum State { Left, Right }

void Start() { //Wanna make this state choose a random State from State. :P So somethimes its left other times its right.... state = State.Left; }

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

4 Replies

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

Answer by DaveA · Apr 04, 2011 at 09:49 PM

Since it's just two choices:

state = Random.value < .5 ? State.Left : State.Right;
Comment
Add comment · Show 2 · 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 Bormeth · Apr 05, 2011 at 06:02 PM 0
Share

Thx m8 worked just fine :) And thx to others too, Tried "state = GetRandomEnum();"

But didnt work.

avatar image DaveA · Apr 05, 2011 at 06:09 PM 0
Share

Just a note: if you have more than two choices, you could use casting, but you should make sure you know the $$anonymous$$ and max values of the enums, and that there are no 'holes' in the sequence.

avatar image
24

Answer by Bunny83 · Apr 05, 2011 at 01:01 AM

Well the two solutions here will work in your case, but since enums are not restricted to sequential values here's a general approach (C#):

I just wrote a little generic helper function that will select a random enum member.

static T GetRandomEnum<T>()
{
    System.Array A = System.Enum.GetValues(typeof(T));
    T V = (T)A.GetValue(UnityEngine.Random.Range(0,A.Length));
    return V;
}

And here's a little example:

enum MyEnum { Dog = 1, Cat = 2, Cow = 502, Bird = 621 }

MyEnum choice = GetRandomEnum<MyEnum>();

And in your case you could do:

state = GetRandomEnum<State>();
Comment
Add comment · Show 9 · 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 Dark_Moo · Apr 30, 2013 at 02:30 AM 0
Share

could you please explain this a bit, mostly what the T means in static T GetRandomEnum()

it works but I do not know what the T is for and why a static is required ins$$anonymous$$d of a class.

avatar image Bunny83 · Apr 30, 2013 at 07:34 AM 0
Share

@Dark_$$anonymous$$oo: This is a generic function. The type-parameter T allows the function to work with different types ins$$anonymous$$d of one. The return type of the function (which is T) will vary on how you call the function.

Unity has also a page which explains generic methods briefly

avatar image Dark_Moo · Apr 30, 2013 at 08:07 PM 0
Share

cool thanks a lot :)

avatar image NoodleSpenny · Mar 09, 2014 at 01:21 PM 0
Share

This is a good answer to the general problem. Thanks!

avatar image FreemanPixelz · May 18, 2014 at 04:54 PM 0
Share

Excellent solution!

Show more comments
avatar image
1

Answer by KeithK · Apr 04, 2011 at 09:21 PM

Give this a bash.

EDIT: Added + 1 to the Max parameter of Random.Range(), as per Peter's comment.

state = Random.Range((int)State.Left, (int)State.Right + 1);
Comment
Add comment · Show 2 · 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 Peter G · Apr 04, 2011 at 09:25 PM 2
Share

Random.Range() is exclusive on the max value, so it will never be chosen if you do that.

avatar image KeithK · Apr 04, 2011 at 09:31 PM 0
Share

Whoops, good call. Will edit my post.

avatar image
0

Answer by Bormeth · Apr 05, 2011 at 06:02 PM

Thx all :) DaveA answer did the job..

Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Rigidbody2d Moving up and Down but not left and right 1 Answer

Skybox - Left and Right is swapped, needs fixing. 1 Answer

Pay animation if joystick pushed to left and another if joystick is pushed to right 2 Answers

how to control player movement with ui buttons? 1 Answer

Sprite tilts left/right depending on how far it is from the center [C#] 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