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 ShroomWasTaken · Jan 31, 2018 at 04:02 PM · gameobjectreferenceinventory systemitem

Can't seem to find a good, working way of instantiating (dropping) items after they've been added to inventory

I got the basics of my item system working, however I can't figure out how to re-instantiate an item from the inventory into the world again in order to drop it.
My current problem is the fact that the prefab variable my ItemData has, is assigned to the instance of the prefab instead of the prefab, so when I destroy the Item GameObject and add the ItemData, the reference which I use to instantiate it is invalid.

alt text



The first line to the left, is the instance of the prefab in the bottom. The second line to the right shows what the Prefab variable is connected to.

And this picture shows how things look when I picked the item up into the inventory. Since the Item instance was deleted, the Prefab variable of the ItemData in the inventory is invalid. The thing is, I don't want it to reference the instance, I want it to reference the prefab.

alt text



What would be a good solution to this problem? I considered a database with all items, but that feels kinda overkill. (My game will most likely have a lot of items)



Here are the fixed working scripts, added them as links to pastebin:
ItemData.cs
Item.cs
Inventory.cs

skarmklipp.png (74.6 kB)
skarmklipp2.png (68.6 kB)
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
1
Best Answer

Answer by TSI25 · Jan 31, 2018 at 04:34 PM

So probably what you want to do is have a serialized class or scriptable object that contains data about the 'item' - stuff like weight or price or anything else you need to track about it. any time this 'item' is in someone's inventory it doesn't necessarily need to exist in the scene, and you can retrieve any data you need from that.


Then you would have this item have a prefab reference to the physical object, so if they chose to drop the 'item' it would instantiate whatever its prefab is, and then be removed from their inventory list (or array or whatever). This prefab would then need a reference to whatever the item data is. if you are using scriptable objects you can set that as a reference, or if you are using a serialized class you can read the data in from the inventory item's data when you instantiate it. It is important that this prefab reference isn't assigned to an object in the scene because when that object is deleted youll get a missing reference like youre seeing there.


ultimately it comes down to maintaining a separation between the data of an inventory item, and the in-game physical representation of the item. This approach will allow you to have a ton of objects in a player's inventory, as well as retrieving and passing that data around without actually needing to have prefabs loaded in the scene - while still allowing you to instantiate prefabs from that data when needed. Let me know if that doesn't make sense.

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 ShroomWasTaken · Jan 31, 2018 at 04:43 PM 0
Share

I had no idea at all ScriptableObjects could be used as assets, I've been thinking they were just some weird Unity-alternative to normal classes. I'm not sure where I got that from, but this fixed the issue. Just had to change about 2 lines and everything started working. Thank you so much. I'll go ahead and add my code to the post in case anyone would like to check out how ti actually works. Again, thank you! :)

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

108 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

Related Questions

Copying GameObjects with reference types 1 Answer

inventory system: drop item with mouseclick 1 Answer

Create usable item with Brackeys inventory 1 Answer

Null Reference at (wrapper managed-to-native) 0 Answers

Reference variable in other script 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