- Home /
Maintaining Object references between Edit and Play mode
I'm writing an Editor Window that lets me configure some ScriptableObject
objects. One of them contains a reference to a GameObject
. If I set that reference through the GUI during edit mode, then when I switch to Play mode the reference has become Null
. When I then switch back into Edit mode the reference has come back again (in other words, I presume that the set of GameObjects
during play mode are not actually the same objects as you see during Edit mode).
Any ideas how to make my objects automatically point to the current references during Play mode if they've been set up in Edit mode?
I invite you to read my thread, as it is very related: http://forum.unity3d.com/threads/107831-EditorWindow-loses-reference-of-ScriptableObject-on-Play-mode
Answer by Rod-Green · Jul 21, 2011 at 01:15 AM
Old question but something I've dealt with recently. Sounds like you are running into a non serialized property.
This can be either a Static or Private property that on play is 'initialized' to it's default value and therefore losing the preplay value.
So one way to fix would be to set the property to [SerializableField, HideInInspector] or make it public.
If it's a static property you'll need to assign it to a local instance property (i.e. every time it's changed) and then serialize that property as mentioned above.
On play however you'll then need to reassign that property back to the static representation.
Answer by Brizores · Jan 24, 2021 at 10:47 AM
I had similar probleb. Solution it is about using PrefabUtility.RecordPrefabInstancePropertyModifications(). Here is a ref https://docs.unity3d.com/ScriptReference/Undo.RecordObject.html,I had this problem. Solution it is about using PrefabUtility.RecordPrefabInstancePropertyModifications(). This allow you to save references bitween objects. Here is a ref https://docs.unity3d.com/ScriptReference/Undo.RecordObject.html
Your answer
Follow this Question
Related Questions
Unity Editor Scripts: GameObject Added / Removed Event? 1 Answer
Adding to GameObject context menu WITHOUT [MenuItem(...)] 1 Answer
how do I generate a drop-down list of functions on scripts attached to a game object? 1 Answer
How can I set the static checkbox of a GameObject in an editor script? 1 Answer
Execute Code When Component Added 6 Answers