- Home /
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.
Answer by fafase · Jan 28, 2017 at 08:55 PM
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);
}
And creating this inventory system with array only ins$$anonymous$$d of enums? It should be less confused as code?
You can consider a dictionary to be an array. Ins$$anonymous$$d of accessing via index you use a key which can be anything type.
$$anonymous$$mh... so the advantage of "Dictionary" is to have more controll but less versatility than arrays?
Ok! Now the problem is How integrate it with the code, but I'll try anyway. Thanks! :)
Hi. I tried to implement It, but I failed. What I should modify in the code I wrote in the post I linked above?
Answer by Ragatto · Feb 17, 2017 at 07:32 PM
@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?)
Your answer
Follow this Question
Related Questions
Ray.direction editing not working in C#? 1 Answer
Where can i find an easy raycast shooting tutorial for C#? 2 Answers
Raycasting to avoid falling (FPS game) 2 Answers
shooting multiple enemies using raycast 2 Answers
Multiple Cars not working 1 Answer