- Home /
Problem with Enum and String, Beginner Problem
Hey Folks!
i use two scripts. my enum part looks like this:
public enum ItemType {
**Sword,
Armor,
HealthPotion,
Dagger,**
}
Now I want to add a new item in my other script. My Problem is that i want to transfer a string instead of Sword, Armor, HealthPotion or Dagger with the same value in it.
For example: My other script has a string variable with the value "Dagger"
GameObject.FindWithTag("gun").name = weaponname;
How can I use the string "weaponname" in the following code? instead of the enum.
inventory.AddItem(new Item { itemType = Item.ItemType.(here i want to use a string instead of Sword, Armor, HealthPotion or Dagger), amount = 1 });
Do you know if this is possible? I would be really happy if you can help me!
Please let me know if you dont understand my problem. it's little bit hard for me to discribe it.
Answer by CodeMonkeyYT · Apr 20 at 07:59 PM
You can cycle through all the values of that enum with Enum.GetValues(typeof(Item.ItemType));
then compare them with your string to find the correct one.
Or you can use Enum.Parse(typeof(Item.ItemType), weaponname);
Hey CodeMonkeyYT,
It Works!! Thank you very much! You are the best and I loved your inventory video!!
Much love <3!!