- Home /
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?
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.
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.
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.
But what about components not derived from $$anonymous$$onoBehaviour? They CAN exist without a GameObject, right? What about those?
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?
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
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.
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.
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.