Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ragatto · Jan 28, 2017 at 06:25 PM · c#raycastfpsinventory systempickups

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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);
 }




Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ragatto · Jan 29, 2017 at 04:30 AM 0
Share

And creating this inventory system with array only ins$$anonymous$$d of enums? It should be less confused as code?

avatar image fafase Ragatto · Jan 29, 2017 at 05:07 PM 0
Share

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.

avatar image Ragatto fafase · Feb 05, 2017 at 09:10 PM 0
Share

$$anonymous$$mh... so the advantage of "Dictionary" is to have more controll but less versatility than arrays?

Show more comments
avatar image Ragatto · Feb 09, 2017 at 09:35 PM 0
Share

Ok! Now the problem is How integrate it with the code, but I'll try anyway. Thanks! :)

avatar image Ragatto · Feb 16, 2017 at 10:26 AM 0
Share

Hi. I tried to implement It, but I failed. What I should modify in the code I wrote in the post I linked above?

avatar image
0

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?)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

292 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges