- Home /
Holding references to other gameObjects
I'm able to do this, but as soon as I reopen the scene or alter the script, it looses the references and I have to use GetComponentsInChildren, which breaks all my event subscriptions.
How do I maintain these references to existing GameObjects?
Answer by duck · Oct 17, 2010 at 08:47 AM
How are you creating the references in the first place?
There are lots of ways to do this, one of the main ones being dragged refereces in the inspector.
Edit:
From the comments, it sounds like you're grabbing all the child objects via scripting, which is suitable for the situation.
If you are switching between scenes at runtime, and you want to preserve this object and the references it holds in its variables, you can use DontDestroyOnLoad().
However, if you edit any script, Unity will recompile all scripts (losing all data currently stored in their instances), so if you do this at runtime, it's generally not really common for any currently running script to keep functioning as expected in that situation. This is sort of a case of just "don't do that".
It's a curve, where control points are components attached to gameobjects, that are parented to the curve gameobject. So it's not a few static references that I could drag into the inspector. The components added are returned to a List in the Curve component.