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 /
  • Help Room /
avatar image
0
Question by Sskethan · Nov 10, 2016 at 08:02 PM · inventorypickupitem pickup

Find the name of item being picked up - C#

Hello, I have worked out how to pick up an item and add it to my inventory, but the method I'm using requires me to specify the objects name. Meaning i could only pickup 1 type of weapon unless i make a separate if statement for each weapon. How could I find the name of the object being picked up and fill in the string name of the weapon. "Find(x => x.ItemName == "Axe");" Thank you all for any help you can provide.

     void OnTriggerEnter2D(Collider2D other)
     {
         if (other.tag == "Weapon")
         {
             GameObject tmp = Instantiate(InventoryManager.Instance.itemObject);
             tmp.AddComponent<ItemScript>();
             ItemScript newWeapon = tmp.GetComponent<ItemScript>();
             newWeapon.Item = InventoryManager.Instance.ItemContainer.Weapons.Find(x => x.ItemName == "Axe");
             inventory.AddItem(newWeapon);
             Destroy(tmp);
         }
     }

Comment
Add comment · Show 5
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 TreyH · Nov 10, 2016 at 08:25 PM 1
Share

Ins$$anonymous$$d of using 'other.tag == "Weapon"', have you considered adding a component that might do exactly what you want?

Suppose you were to add some Weapon component to your project and added this to every item you are currently tagging manually as "Weapon". You could ins$$anonymous$$d check to see if your collision object has that Weapon component and then use some public string (in this case, "Axe") to get its name.

avatar image ElijahShadbolt · Nov 11, 2016 at 01:59 AM 0
Share

When you call Destroy(tmp);, doesn't it destroy the GameObject that the newWeapon is attached to? That would mean newWeapon will be null in the next frame.

avatar image Dibbie · Nov 11, 2016 at 02:32 AM 2
Share

Your on the right track, use an if statement on "other", since thats what was interacted with.

if(other.gameObject.name == "Axe") or if(other.gameObect.name.StartsWith("Axe")) or if(other.gameObject.name.Contains("Axe")) could all be alternatives, depending on how you want to check for things.

avatar image Sskethan · Nov 11, 2016 at 02:37 AM 0
Share

TreyH - Thank you I will look into this.

SublimeGamer - newWeapon is an empty gameObject that collects the stats from the xml for the weapon and once placed in the inventory is no longer needed.

avatar image ElijahShadbolt · Nov 11, 2016 at 03:37 AM 0
Share

It looks like you're adding an ItemScript component to a GameObject and making a reference to it called newWeapon. When a GameObject is destroyed, so are all of its components, so the reference you added to the list will be null, because adding a Component to a list of Components just adds a reference to the Component, not a copy.

1 Reply

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

Answer by Sskethan · Nov 11, 2016 at 09:43 PM

Thank you everyone. I got it now. I just needed to use other.gameObject.name.

     void OnTriggerEnter2D(Collider2D other)
     {
         if (other.tag == "Weapon")
         {
             itemName = other.gameObject.name;
             GameObject tmp = Instantiate(InventoryManager.Instance.itemObject);
             tmp.AddComponent<ItemScript>();
             ItemScript newWeapon = tmp.GetComponent<ItemScript>();
             newWeapon.Item = InventoryManager.Instance.ItemContainer.Weapons.Find(x => x.ItemName == itemName);
             inventory.AddItem(newWeapon);
             Destroy(tmp);
         }
     }
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

80 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

Related Questions

Inventory Master 0 Answers

RPG Picking up items with polymorphism - probably quick :) 1 Answer

Adding a prefab to the Item class instance 0 Answers

Pickup Script Problem 1 Answer

Creating inventory system with pickup problem 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