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 DaKocha · Apr 02, 2016 at 03:35 PM · gameobjectscomponents

Component references when GameObject destroyed?

Hello! Here's my situation - I stumble on an item (say) with an Item component attached to it. I indicate the player that pressing 'e' will pick it up. He hits 'e'. In my code, I've been waiting for this to happen and so it triggered this function that'll first fetch the item type and add a reference to the respective item Dictionary. after that is DONE, we no longer need the GameObject and therefore call Destroy() on it.

Now comes the trick part, if I destroy it, the components will be gone with their master (the GameObject), which, I assume, would make those references in the Dictionary hopeless!! That's the crux.

Now I've this strange feeling that there exist this method that disconnects the component from the GameObject (or is it just my crazy mind?).

Can you people please show me the way?

TIA

P.S also, where's the offline docs? its a download, right?

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

2 Replies

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

Answer by meat5000 · Apr 02, 2016 at 04:31 PM

If destroying the gameobject is purely for cosmetic reasons, consider disabling its renderer instead.

Create a struct with the relevant variables in it which match what data it is you are trying to record. Make a List of Type YourStruct and store the info you want from that object's components in it.

Then you can destroy the object.

Comment
Add comment · Show 5 · 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 DaKocha · Apr 02, 2016 at 04:34 PM 0
Share

Yes, cosmetic reason :) but disabling the renderer will keep the thing in memory.. not good, right? But yes, creating a struct is my only hope right now. Thank you all for your time.

avatar image meat5000 ♦ DaKocha · Apr 03, 2016 at 07:29 AM 0
Share

You can remove a component from an object but not separate it. A component can only exist within an GameObject. You can take note of what component it is and add it to another object but it will be a fresh instance, of course. So you cant store its information that way.

avatar image DaKocha · Apr 03, 2016 at 09:14 AM 0
Share

But what about components not derived from $$anonymous$$onoBehaviour? They CAN exist without a GameObject, right? What about those?

avatar image meat5000 ♦ DaKocha · Apr 03, 2016 at 09:38 AM 0
Share

Unattached scripts are like unused lego. You need to declare them in an attached script to make them do anything. What exact are you trying to do with them?

avatar image DaKocha DaKocha · Apr 03, 2016 at 01:28 PM 0
Share

Err... keep a live reference to them even after the Host GameObject is destroyed :) by the way, I forgot that non-$$anonymous$$onoBehaviour derived code cant be attached to gameObjects, lol. I think I'm in an awkward situation, and I implemented much more awkward algorithms to address those haha

avatar image
1

Answer by malta32 · Apr 02, 2016 at 04:08 PM

Could you try separating the GameObject and Item classes? Store the items in a master Dictonary and create a GameObject that has a script that has an int(itemID) that represents the items key in the item dict. When you press 'e' callout to the item dictionary using the item itemID stored on the GameObjects script and add a new item created from the item dict to your inventory. Then destroy the gameobject.

Comment
Add comment · Show 2 · 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 DaKocha · Apr 02, 2016 at 04:20 PM 0
Share

Well, if I go that route, things will go all bloated? I'm storing each picked up item in each caches, Dictionaries. Actually, my Item class has some data in it. So I cant just create an Item object and init it. Or could I? I'm confused. Isnt there any method to detach components from GameObjects? By the way, the Item class isn't derived from $$anonymous$$onoBehaviour.

avatar image DaKocha · Apr 02, 2016 at 04:36 PM 0
Share

Your answer and the next answers are essentially the same. I marked his as the accepted one. :) But yes, creating another class/struct is the thing.

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

41 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

Related Questions

Attach the ObjectManipulator MRTK component in runtime to instantiated GameObject? 0 Answers

Opeartions on GameObjects before scene is loaded 0 Answers

Get a GameObject from a component 1 Answer

handle gameobject Component in c# 3 Answers

Need a persistent unique id for gameobjects 11 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