- Home /
Keeping native data around across recompile
I think I finally get how unity works with serialization etc. So, for example if I have a MonoBehaviour
script and while in the editor, while the game is running, I edit the script, Unity will serialize that object, destroy it, load the new code, create a new object, deserialize the data back in. Great!
But what if some of the objects I'm referencing are native objects? An easy example might be System.Net.WebRequest
(yes, I know about WWW
that's not the point!). How can I get that object to survive across re-compilations?
Assume I have some code like this
public MyObj : MonoBehavior {
private System.Net.WebRequest request;
void Start() {
request = new System.Net.WebRequest.Create(...);
}
void Update() {
Debug.Log(request.ContentType);
}
}
The moment I compile something I'll start getting errors from Update
because request will be null. The original request still exists as it's still handling the request but Unity can't hook it back up. How I can work around Unity's serialization here to persist native objects across re-compiles?
I can imagine making some kind of ScriptableObject, keeping List or Dictionary of the native objects in some maybe static property, serializing Ids and some how restoring the connection between objects across re-compiles? Would that even work?
What other options are there?
Your answer
![](https://koobas.hobune.stream/wayback/20220613173416im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity Not Loading 0 Answers
SerializationException: Type UnityEngine.UI.Button is not marked as Serializable. Solution 1 Answer
Saving players progress in ScriptableObject asset 1 Answer
Make runtime-instantiated game objects persistent OR make their unique ID persistent 0 Answers
Cannot save(serialize) a double array of a custom class 1 Answer