Question by
Bokaii · Aug 14, 2016 at 05:00 PM ·
c#controllerenum
Unity3d select random enum in another script
I have an info script, where I have name, type etc. Type is an enum, and from 1 script I want to randomly select what type the object is.
But I can't seem to figure out, how to randomly select an enum in another script..
Comment
Answer by EDevJogos · Aug 15, 2016 at 01:33 AM
Do a Random.Range() and cast it to the enum type:
Each entry in the enum has its own index value in this case ITEM_01 = 0, ITEM_02 = 1, and so on...
public enum ItemsType
{
ITEM_01,
ITEM_02,
ITEM_03
}
private ItemsType randomType;
void Start()
{
randomType = (ItemsType)Random.Range(0, 2);
}
Your answer
Follow this Question
Related Questions
help to connect two objects together 0 Answers
C# missingReferenceException when comparing enums 0 Answers
Problem assigning enum value 1 Answer
Clicking a UI button using a xbox one pad? 0 Answers
Some problems with handmade basic player controller 0 Answers