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 /
This question was closed Jul 29, 2018 at 10:30 AM by bl4cksky for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by bl4cksky · Jul 27, 2018 at 04:38 PM · referencing

reference to different classes : item held by different type of owner

Hello everyone,

It's probably answered somewhere else but i couldn't formulate my question so that a search could find the answer.

Here's the thing : i'm ocding a simple 2D game seen from above. I'm imlplementing different types of items such as materials, tools, and the objects the character can use, called "Inventory" in my code. Such inventory can be either held by a character or lying on the ground, so both the classes "Tile" and "Character" have a "Inventory" reference. But the inventory needs to know who owns it. And i don't know how to do it elegantly.

what i did before was to have simply 2 references, one of type "Character" and one of type "Tile", but since other type of owner will add in the future this add more and more lines of code, and the checks will be quite painful to implement...

DO you know a way of solve this riddle? Thanks in advance, Gabriel

Comment
Add comment · Show 4
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 David_Rios · Jul 27, 2018 at 05:03 PM 0
Share

Hey Gabriel, will the inventory script initialize as a component of a gameObject or as a component of a different 'Inventory' gameObject?

avatar image bl4cksky David_Rios · Jul 27, 2018 at 05:32 PM 0
Share

the way i did, inventory is not linked at all to a gameobject, it's just a script. The gameobject have a script called "InventoryController" that interprets the "Inventory" scritp attached to it to render it. This way when i save i store only the values of the model.

avatar image David_Rios bl4cksky · Jul 27, 2018 at 06:05 PM 0
Share

So what you can do is get the gameObject of the inventoryController script by just using something like private GameObject owner = gameObject

And then use that owner reference from your inventoryController script inside of your Inventory script.

Show more comments

2 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by bl4cksky · Jul 27, 2018 at 07:15 PM

I'll look at it. And could an interface do the work?

I used intefaces very little, but i could implement a "CanStockInventory" interface and have "Character" and "Tile" have it? And the inventory reference would be in the interface?

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 bl4cksky · Jul 29, 2018 at 10:29 AM 0
Share

Okay i solved it from another way. I implementaed an InventorySlot class that has all the ownership reference, except that it shouldn't change since it's attached to whatever tile or character that can have an inventory, so the functions to change ownership of inventory just applies to the slot the inventory is attached to. This way the code is simpler and i can freely move inventories all around.

Thanks for the answers Carterryan1990 and BDCakerules

avatar image
0

Answer by Carterryan1990 · Jul 27, 2018 at 06:30 PM

You should look up script-able objects. I think that may help solve your issue. If your destroying the object a script-able object being destroyed wont remove any of the properties or inventory items.

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

Follow this Question

Answers Answers and Comments

88 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

Related Questions

Referencing all objects in scene 2 Answers

trouble with referencing a componenet (bool) 1 Answer

Instantiated Objects dont get their Script referenced properly 2 Answers

why doesn't my reference work? 1 Answer

Using a Scriptable Object as a Config 0 Answers


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