- Home /
How to make a drop down list in inspector
Hello , i was searching all over the internet on how to solve this. I was looking in tons of questions/answers and none of them worked for me. I want to make a list in inspector exactly the same as this guy have. I tried with enum listing , like
public enum List
{
item1,
item2
};
public bool Asd;
public class Example {
Asd = List.item1;
}
But didn't work out , says i can't assign bool to variable enum. So if anyone can please help i would be grateful.
Answer by Screenhog · Sep 18, 2012 at 09:34 PM
It's pretty easy, although enum's probably not your answer.
You need to make a class, and then reference it. Here's a simple one:
class Person {
var name: String;
var age: int;
var favoriteFoods: String[];
}
Then, in any other code, reference it, something like this:
var teacher: Person; //one person
var students: Person[]; //a built-in array of people
Note that this is Unityscript, not C#. I believe it's similar in C#, though.
In case you're curious, an enum is for pre-making a list that you'll want to reference later. For instance, something like this:
enum Gender {Male, Female}
enum RPSChoice {Rock, Paper, Scissors}
You'd then reference it later:
var playerGender: Gender;
This would give you the choice in the inspector of Male or Female, and in code, it would be a choice of Gender.Male or Gender.Female.
I need it in C# , can't find it anywhere. I have a script done , 560 lines but all i want is to make the variables looks nice in Editor. Nothing worked , not even the class
Answer by gminitv · Oct 13, 2013 at 10:32 PM
You want something like this:
public enum State{
walking,idle,attacking,eating,sleeping
}
public class AClass:MonoBehaviour{
public State current;
}
Use enumerators and for syntax reference any C# syntax doc or tutorial doesn't need to be Unity3D specific. Using enumerators gives you a drop down in Inspector like you're looking for. This answer came from another source because I wanted to find an easy way to explain in better words than my own and felt the wheel need not be re-invented :)
Link to original post on answers.unity3d.com/questions with this solution
PS: I am not fluent in C# language but you may not be able to store a boolean as an enumerator. In that case it would be a "type miss-match" error most likely because the Boolean Object may not be able to be stored as an Enumerator Object. In context imagine you have a list of cars and ONLY cars are allowed to be part of the list. If you say you want to add a Banana (which is a fruit not a car) to the list of Cars with this same logic you wont be able to. Only cars are allowed in the cars list (cars object no banana objects or other object types allowed) This is the same case when trying to store and integer as a string or a string as an integer. Usually there is some form of method or function or something you can "call" or reference to convert objects of different types. Usually this looks like this : String number = Number.getValueOf.toString() (no particular syntax just explaining in a code type form) You could Google search for "How to convert a boolean to enumerator object" or "how to store a boolean as an enumerator" or possibly just google search the EXACT error that your compiler is giving you about why it will not allow you to store the boolean as an enumerator.
I would imagine alternatively you could create a custom class that does exactly this. converts true/false yes/no booleans into an enumerator. Just create a class that has a method/function like this: convertENUMS(String s, Int i, Enum e, Boolean B); and then call this method when you need to store the boolean as enum or any other conversion you may need. If you convert the boolean to a string first called "yes or no" or maybe "hair, nohair" or "Gravity, NoGravity" or "Hit,Miss" then just do simple if/then (if ( ourstring != "hit" ) then { print('we have a MISS!') }
Your answer
![](https://koobas.hobune.stream/wayback/20220613081205im_/https://answers.unity.com/themes/thub/images/avi.jpg)