Modifing an Enum value by a String?

Hi again.

I wrote some enum and I inserted them inside 6 arrays(one new enum for each array).
You can find my script here: Inventory script topic

I created another script named Interaction: this should pickup a weapon and place it In a specific slot if empty, else if busy change weapon dropping the hold one from the slot.

So I made a raycast and a CompareToTag(“Weapons_MELEE”), if the raycast hits a gameObject with this tag:

            GameObject inventory = GameObject.Find("Weapons");
            WeaponSelection inventoryScript = inventory.GetComponent<WeaponSelection>();
            pickedObj = hit.transform.name;
            inventoryScript.storedWeapon = inventoryScript.inInventory[inventoryMelee.pickedObj];

But it obviously doesn’t work.
What I wanted to do is, as you probably realized from the code I pasted above, is setting the array content by insert the name of the gameObject picked up, so if the array based on enum contents, for example “Hammer”, the stored weapon in that slot equals to Hammer and it can be equipped by pressing 1.

I’m not a expert coder, I’m trying to learning.
Maybe It’s wrong using enums in this case? Maybe using array could be more simple?

Thanks.

I guess you want a dictionary of string and game object

Dictionary<string, GameObject> dict;

void Start()
{
      dict = new Dictionary<string, GameObject>();
      dict.Add("Axe", axeGo);  // Find the axe object either by editor or by code
      dict.Add("Knife", knifeGo);
}
public GameObject GetFromDictionary(string obj)
{
     if(dict.Contains(obj) == false){ return null;}
     return dict[obj];
}

And you use:

void PickeUpObject()
{
      GameObject inventory = GameObject.Find("Weapons");
      WeaponSelection inventoryScript = inventory.GetComponent<WeaponSelection>();
      pickedObj = hit.transform.name;
      inventoryScript.storedWeapon = inventoryScript.GetFromDictionary(inventoryMelee.pickedObj);
}

@fafase the error is weaponSelection.INVENTORY_Melee’ does not contain a definition for ‘pickedObj’ and no extension method ‘pickedObj’ accepting a first argument of type ‘weaponSelection.INVENTORY_Melee’ could be found (are you missing a using directive or an assembly reference?)