- Home /
Adding elements to an enum at runtime
Is it possible to add elements to an enum at runtime?
Answer by Owen-Reynolds · Jan 19, 2012 at 03:32 PM
Enums are almost always used to help read if/switch logic. Something like:
enum doorType { open, closed, locked, broken, UNKNOWN };
if(door1.status == doorType.locked) {
AddMessage("Locked"); PlaySound(knobJiggle); }
else if(door1.status == doorType.closed) {
door1.status=doorType.open; door1.animation.Play("opening"); }
If you wanted to add a new value, like gluedShut, you'd need to add new ifs to handle it, so adding at runtime wouldn't be very helpful. If you want something you can add to, try an array, List or Dictionary. Can then say things like myStuff.Add("hammer");
ya but you can also use enum as named constants... just saying ...
As i said even adding an enum at run time wont help a programmer..
here is is a good link on this sort of idea
https://stackoverflow.com/questions/2779743/can-you-add-to-an-enum-type-in-run-time
hope it helps.
Answer by flamy · Jan 19, 2012 at 01:39 PM
simple answer is no... you can't do it
enum is jus like class or struct ... do u think it is possible to add a new member or function to a class at run time??
also enums are supposed to be a complete static enumeration. Even it is possible to do it... i dont think it is worth all the effort?!! Are u just trying out of ur intrest?!
Answer by larex39 · May 07, 2020 at 07:16 AM
Here is youtube tutorial how to show List as Enum in inspector https://www.youtube.com/watch?v=ThcSHbVh7xc