Hey everyone
Need a little help with this one please, this script works fine but its a pain when i have to implement more things into the script. I wondered is there a way to optimise the script using the enum to but directly into an int without having to set to string each time?
public enum chosenitem
{
Zero, One, two, three, four, five, six, seven, eight, nine, ten, eleven
}
public class Droppeditem : MonoBehaviour
{
public chosenitem chitem;
public string chositem;
public int itemid;
itemdatabase database;
void Start()
{
chositem = chitem.ToString();
database = GameObject.FindGameObjectWithTag("Itemdatabase").GetComponent<itemdatabase>();
if (chositem == "Zero")
{
itemid = 0;
}
if (chositem == "One")
{
itemid = 1;
}
if (chositem == "Two")
{
itemid = 2;
}
if (chositem == "Three")
{
itemid = 3;
}
if (chositem == "Four")
{
itemid = 4;
}
if (chositem == "Five")
{
itemid = 5;
}
if (chositem == "Six")
{
itemid = 6;
}
if (chositem == "Seven")
{
itemid = 7;
}
However, I don’t really why you’re using an enum in the first place. The point of enums is to avoid magic numbers, but your enum is just spelling the numbers out, so there doesn’t seem to be much point.
Yes perfect thank you guys, I used your version eric it works.
Basically so i can select a object type in the inspector on an object which links to a list, mainly for inspector use tbh not sure how else to create a drop down for the inspector
Edit - I also made a mistake in my question was meant to say convert to int not string glad you understood what i meant
Thanks for the replys people, i think mainly the issue due to my lack of knowledge on enums tbh I havent used them much. I didnt realised you could do .Zero so I was converting it to a string and working it that way, a long winded way I know but its much better now.
So now when I use the itemid = (int)chosenitem; and select the item i need in the inspector before runtime it puts the correct itemID in and it searches my list and so on…
See I needed a quicker way as everytime I added an item to my list I had to add the conversion in the string you see…
serialization - you serialize your enum as an integer. Your serialize method should handle the conversion back and forth in an encapsulated place so that it is typed ‘int’ for only that part of the process.
inspector/labels/etc - you want to show a dropdown box, or list, or something visual of the names of each enum value. Say in the inspector (of course unity supplies a function that does this for you, so you shouldn’t really need to convert to string unless writing a highly specialized editor/inspector)
to perform some arithmetic or boolean logic on the enum for some reason. This would mean your enum is probably non-sequential, and highly specialized.
Other than that though, I have a hard time coming up with off cases that it might be necessary. The whole point of an enum is so that you don’t have to keep referring to the value as it’s integer value. So you don’t even need to KNOW the related integer value. It just works by name.
You’ve identified the issue, so why not rectify it? It won’t take long to learn about enumerations, and once you’ve learned about them you’ll not only be better equipped to solve this problem, but you’ll also know how to better use a tool that will continue to be useful in the future.
You’ll need to do this kind of spot learning all the time if you’re going to do much programming.