• 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 Desfell07 · Nov 17, 2019 at 02:06 PM · inventoryinstances

Make a instance of a script passed with prefab

Hello, the title says it all. I have a invenotry system, pretty straightforward - slots that hold item. This is how it looks like. alt text

I just assign the starting items with prefabs, that are just an empty object, which contains the WeaponItem class and has already predefined values. But some of those are runtime - such as currentBullets currentMagazines or isReloading and when they change, they change it on the prefab and so that for example all the MP40's affect all the other MP40's in the level.

How could I go about making each WeaponItem it's own instance / severing it's connection to the prefab, so it does not affect the prefab iself? This is what I have tried so far alt text

Thanks for reading!

bez-nazvu.png (45.3 kB)
bez-nazvu2.png (48.1 kB)
Comment
Add comment · Show 1
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 Desfell07 · Nov 17, 2019 at 04:28 PM 0
Share

O$$anonymous$$, the final solution was to make the items scriptable objects. Altough it is not recommended, I have to instantiate them due to the problem mentioned above, they actually work without creating object in game.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by prof · Nov 17, 2019 at 08:47 PM

If you don't need object to be in scene, use ScriptableObject instead. And yes, you need to make new instance or copy values manually if you're going to change data, so original stays same.


Also, you really should break up static and dynamic data. Put your data that contains settings (that will not change during runtime) in separate class/struct

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 Desfell07 · Nov 18, 2019 at 08:42 PM 0
Share

Yes, creating scriptableObject and instantiating that finally worked, thank you! And for the static and dynamic data, I think of moving the static data into the WeaponReferenceBase, which already contains weaponID, sound and also references such as the weapon model in hand to turn off when switching weapons etc. Thanks for the advice!

avatar image
0

Answer by Desfell07 · Nov 17, 2019 at 03:23 PM

I thought some details might be helpful to note This is the InventorySlot, WeaponItem code and the prefab I am using to fill in the inventory itself. It all comes down to making the InventoryItem in InventorySlot an instance. alt text


code.png (205.6 kB)
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

117 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

Related Questions

Inventory Right Click: NullReferenceException: Object reference not set to an instance of an object 1 Answer

check if list contains item with matching string property 2 Answers

..."Cannot be accessed with an instance reference, qualify it with a type name instead" error 1 Answer

Save data in Single-Player 1 Answer

loop index error? JS 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