Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Magnomous · Jun 29, 2015 at 01:29 PM · c#modeliteminstances

Do I need to rework whole item system?

Hello, I'd like hear from you some ideas. This is the problem:

I've made base class Item. It has some fields (e.g. name, itemModel, icon, etc), methods and virtual method CopyYourself() which returns instance of itself.

Then there are other classes like Weapon, Armor, Shield, ... Weapon has fields like damageValue and Armor has armorValue. All of these classes override CopyYourself() and return themselves.

Then I've made script ItemDatabase which inherits from MonoBehaviour. It is attached to GameController GameObject. This script includes few generic lists. E.g.:

 public static List<Weapon> weapons;

In Awake() method I've added some weapons into this list. E.g.:

 weapons.Add(new Weapon(6, WeaponType.One_Handed, "Basic Sword", LoadObject(_WEAPONS_PATH, "Basic Sword"), LoadIcon("Basic Sword"), 3, 2, 1));

(variables in the constructor are not part of the problem, so I won't explain them). But the part LoadObject(_WEAPONS_PATH, "Basic Sword") important is. LoadObject is function located in ItemDatabase script. It returns GameObject found in "Resources" folder.

I've even made inventory and all of it nicely worked. I can add items there, equip them etc. Inventory is fully functional.

I've made some enemies and all of them have method Death(). Once they die, they drop items. Works perfectly...

They drop items so, that they check their list of items that can drop from them. In this list are items from ItemDatabase. It uses CopyYourself() function to make a copy of this item from database (and sometimes makes some changes of this instance) and then instantiates its itemModel. Also, works perfectly.

But here is the problem: When player wants to pick them up from the ground, this itemModel has no idea that it is weapon, armor or even that it is actually Item. Yes, I can tag all of these models as e.g. Item, but the thing is that even if I do that, it will not know what damage this item has, what icon this item has, etc.

And now I am desperate what to do... I would not like to delete all of this... But maybe it is necessary? Maybe I MUST do it so, that every itemModel has attached it's own script to it that defines its damage, icon, etc. ? Or how could I make it so, that after dropping this item it knows that it is weapon with damage x, with icon y?

I appreciate really ANY help and I thank to all of you in advance.

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

1 Reply

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

Answer by Cherno · Jun 29, 2015 at 01:57 PM

Create a new MonoBehavior script called "ItemHolder". This script will have one variable only: An instance of the Item class. All item model prefabs will have this script on them, with the Item variable of the ItemHlder holding the corresponding Item values. When you pick up an item, you access this component and copy the Item values from the ItemHolder script to the inventory slots or whatever, and destroy the gameobject. When you drop an item, you do it the other way around: You instantiate the prefab, access it's ItemHolder component, and copy the Item values from the inventory to the ItemHolder (only neccessary if the values can change while inside the inventory, as the prefab will already hold the default Item values).

Comment
Add comment · Show 1 · 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 Magnomous · Jun 29, 2015 at 02:04 PM 0
Share

Yeah and I can also attach this ItemHolder to item$$anonymous$$odel in Item constructor... Thank you very much man, that was exactly what I needed to know!

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

22 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Splitting Procedurally Generated Mesh, Based On Height 0 Answers

Storing gameObjects in an array and essentially printing them? 1 Answer

Obtaining the relative size of a Models body part 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