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 mmangual_83 · Dec 11, 2013 at 01:51 PM · c#guirandom

Randomize an enum list

I have an enum list that is going to have a different task each time I run the app. But I do not know how to set the enum list to be random. Can anyone show me how to do it? Here is my code:

 using UnityEngine;
 using System.Collections;
 
 [ExecuteInEditMode]
 public class myTestClass : MonoBehaviour {
 public enum MyTasks { task1, task2, task3, task4, task5, task6 }
     MyTasks currentTask = MyTasks.task1;
     public Rect GUIRectWindow, r_msg;
     void Awake()
     {
         //TODO: Initialize stuff here
     }
     void OnGUI()
     {
         // Make a background box
         GUI.Box(GUIRectWindow, "");
         switch (currentCubeTask)
         {
             case CubeTasks.task1:
                 drawLabel(r_msg, "This is task 1");
                 break;
             case CubeTasks.task2:
                 drawLabel(r_msg, "This is task 2");
                 break;
             case CubeTasks.task3:
                 drawLabel(r_msg, "This is task 3");
                 break;
             case CubeTasks.task4:
                 drawLabel(r_msg, "This is task 4");
                 break;
             case CubeTasks.task5:
                 drawLabel(r_msg, "This is task 5");
                 break;
             case CubeTasks.task6:
                 drawLabel(r_msg, "This is task 6");
                 break;
         }
     }
 }
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Sisso · Dec 11, 2013 at 01:54 PM

http://docs.unity3d.com/Documentation/ScriptReference/Random.html

http://msdn.microsoft.com/en-us/library/system.enum.getvalues(v=vs.110).aspx

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 mmangual_83 · Dec 11, 2013 at 01:58 PM 0
Share

Yeah I got that far but my code still gives me errors for trying to explicitly use the Random.Range for the enum CubeTasks. I just want to know the way to use Random.range on the enum list

avatar image
0

Answer by TonyLi · Dec 11, 2013 at 02:02 PM

Under the hood, enums are ints that start at 0 by default (although you can change this). You can cast back and forth, and use Random to select a task randomly:

 public enum MyTasks { task1, task2, task3, task4, task5, task6, Count }
 MyTasks task = (MyTasks) Random.Range(0, MyTasks.Count);

I added a fake enum "Count" to serve as a shortcut for the Random.Range() function.

However, if you want to shuffle a list of tasks, you'll have to do a little more. See andeee's forum thread for some guidance and code. In brief, you'll fill an array with tasks 1-6 and then shuffle the order of the elements.

Comment
Add comment · Show 4 · 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 mmangual_83 · Dec 11, 2013 at 02:10 PM 0
Share

Good find but it's still giving me errors.

Error 2 Argument 1: cannot convert from 'myTestClass.$$anonymous$$yTasks' to 'int'

Error 3 Argument 2: cannot convert from 'myTestClass.$$anonymous$$yTasks' to 'int'

Error 1 The best overloaded method match for 'UnityEngine.Random.Range(int, int)' has some invalid arguments

I don't think my issue is as complex as it is in the forum.

avatar image Guts · Dec 11, 2013 at 03:14 PM 1
Share

Try casting to an int:

 $$anonymous$$yTasks task = ($$anonymous$$yTasks) Random.Range(0, (int)$$anonymous$$yTasks.Count);
avatar image mmangual_83 · Dec 11, 2013 at 03:21 PM 0
Share

@Guts: That worked, thank you!

avatar image RadioactiveXP · Jan 08, 2014 at 05:47 PM 0
Share

This only works (casting to an int) if the enumerations do not explicitly set their values for example:

enum test { none=0, two=2, four=4, six=6 }

If random.range returns 1,3, or 5; it is invalid. The helper code posted with the singleton is a good way to go when that is the case.

avatar image
0

Answer by Hyrne · Dec 11, 2013 at 04:21 PM

helper Code:


 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class EEnum : Singleton<EEnum>
 {
     public T GetRandomEnum<T>()
     {
         System.Array A = System.Enum.GetValues(typeof(T));
         T V = (T)A.GetValue(UnityEngine.Random.Range(0,A.Length));
         return V;
     }
 }
 



Usage:

 public Prefix             _Prefix;  
 
 TMP_Name._Prefix = EEnum.Instance.GetRandomEnum();
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

21 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

Related Questions

Distribute terrain in zones 3 Answers

C# Random Number on GUIButton click without updating every frame 0 Answers

How To Print Text Random 2 Answers

Multiple Cars not working 1 Answer

Why cant I scroll Horizontally in GUI.BeginScrollArea when i set GUIStyle params to null ? 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