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);
}
}
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.
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.
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.
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.
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.
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);
}
}
Your answer
Follow this Question
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